private byte[] toBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data return bytes; }
public String body() { prepareByteData(); // charset gets set from header on execute, and from meta-equiv on parse. parse may not have happened yet String body; if (charset == null) body = Charset.forName(DataUtil.defaultCharset).decode(byteData).toString(); else body = Charset.forName(charset).decode(byteData).toString(); ((Buffer)byteData).rewind(); // cast to avoid covariant return type change in jdk9 return body; }
@Override public ByteBuffer asByteBuffer(int index, int length) { checkIndex(index, length); ByteBuffer duplicate = this.byteBuffer.duplicate(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = duplicate; buffer.position(index); buffer.limit(index + length); return duplicate.slice(); }
int blockSize = 32 * 1024; ByteBuffer bb = ByteBuffer.allocateDirect(blockSize); FileChannel out = new FileOutputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (out.write(bb) < 1) throw new AssertionError(); } out.close(); long start = System.nanoTime(); FileChannel in = new FileInputStream("deleteme.dat").getChannel(); MappedByteBuffer map = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); in.close(); long end = System.nanoTime(); System.out.printf("Mapped file at a rate of %.1f MB/s%n", 1024 * 1e9 / (end - start));
public static void main(String... args) throws IOException { for (int i = 512; i <= 2 * 1024 * 1024; i *= 2) readWrite(i); } private static void readWrite(int blockSize) throws IOException { ByteBuffer bb = ByteBuffer.allocateDirect(blockSize); long start = System.nanoTime(); FileChannel out = new FileOutputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (out.write(bb) < 1) throw new AssertionError(); } out.close(); long mid = System.nanoTime(); FileChannel in = new FileInputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (in.read(bb) < 1) throw new AssertionError(); } in.close(); long end = System.nanoTime(); System.out.printf("With %.1f KB block size write speed %.1f MB/s, read speed %.1f MB/s%n", blockSize / 1024.0, 1024 * 1e9 / (mid - start), 1024 * 1e9 / (end - mid)); }
try (FileChannel in = new FileInputStream(inFile).getChannel(); FileChannel out = new FileOutputStream(outFile).getChannel()) { ByteBuffer buff = ByteBuffer.allocate(8192); int len; while ((len = in.read(buff)) > 0) { buff.flip(); out.write(buff); buff.clear(); } }
// 4k buffer size. static final int SIZE = 4 * 1024; static byte[] buffer = new byte[SIZE]; // Fastest because a FileInputStream has an associated channel. private static void ScanDataFile(Hunter p, FileInputStream f) throws FileNotFoundException, IOException { // Use a mapped and buffered stream for best speed. // See: http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly final FileChannel ch = f.getChannel(); long red = 0L; do { final long read = Math.min(Integer.MAX_VALUE, ch.size() - red); final MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, red, read); int nGet; while (mb.hasRemaining() && p.ok()) { nGet = Math.min(mb.remaining(), SIZE); mb.get(buffer, 0, nGet); for (int i = 0; i < nGet && p.ok(); i++) { p.check(buffer[i]); //size += 1; } } red += read; } while (red < ch.size() && p.ok()); // Finish off. p.close(); ch.close(); f.close(); }
FileOutputStream outputStream = new FileOutputStream(file); int written = 0; try { FileChannel localfileChannel = outputStream.getChannel(); byte[] bytes = new byte[4096 * 4]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int read = inputStream.read(bytes); while (read > 0) { byteBuffer.position(read).flip(); written += localfileChannel.write(byteBuffer); checkSize(written); read = inputStream.read(bytes); localfileChannel.force(false); } finally { outputStream.close();
/** * Retrieves the content of a sent file and saves it to a temporary * file. The full path to the saved file is returned. */ private String saveTmpFile(ByteBuffer b, int offset, int len) { String path = ""; if (len > 0) { try { TempFile tempFile = tempFileManager.createTempFile(); ByteBuffer src = b.duplicate(); FileChannel dest = new FileOutputStream(tempFile.getName()) .getChannel(); src.position(offset).limit(offset + len); dest.write(src.slice()); path = tempFile.getName(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); } } return path; }
int values = 1000000; ByteBuffer buffer = ByteBuffer.allocateDirect(4 * values).order(ByteOrder.nativeOrder()); Random random = new Random(1); long start = System.nanoTime(); for (int i = 0; i < values; i++) buffer.putInt(random.nextInt()); buffer.flip(); FileOutputStream fos = new FileOutputStream("/tmp/random.ints"); fos.getChannel().write(buffer); fos.close(); long time = System.nanoTime() - start; System.out.printf("Took %.3f seconds to generate&write %,d values%n", time / 1e9, values);
final BufferedInputStream inputStream1 = new BufferedInputStream( new FileInputStream( new File(fileName1)), myBufferSize); if (first.size() != seconde.size()) { return false; ByteBuffer secondBuffer = ByteBuffer.allocateDirect(myBufferSize); int firstRead, secondRead; while (first.position() < first.size()) { } finally { if (first != null) { first.close(); seconde.close(); if (first.limit() != second.limit() || length > first.limit()) { return false; first.rewind(); second.rewind(); for (int i = 0; i < length; i++) { if (first.get() != second.get()) {
FileOutputStream outputStream = new FileOutputStream(file); FileChannel localfileChannel = outputStream.getChannel(); byte[] bytes = new byte[4096 * 4]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int written = 0; while (read > 0) { byteBuffer.position(read).flip(); written += localfileChannel.write(byteBuffer); checkSize(written); read = inputStream.read(bytes); localfileChannel.force(false); localfileChannel.close(); size = written; if (definedSize > 0 && definedSize < size) {
FileChannel fc = new FileInputStream(file).getChannel(); ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size()); while (bb.remaining() > 0) fc.read(bb); fc.close(); bb.flip(); // choose the right endianness ShortBuffer sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
ByteBuffer myByteBuffer = ByteBuffer.allocate(20); myByteBuffer.order(ByteOrder.LITTLE_ENDIAN); FileChannel in = new FileInputStream("sample.bin").getChannel(); in.read(myByteBuffer); myByteBuffer.flip(); ShortBuffer myShortBuffer = myByteBuffer.asShortBuffer(); myShortBuffer.get(payload); System.out.println(Arrays.toString(payload));
Charset utf8 = Charset.forName("UTF-8"); CharsetEncoder encoder = utf8.newEncoder(); char[] array = new char[1]; CharBuffer input = CharBuffer.wrap(array); ByteBuffer output = ByteBuffer.allocate(10); for (int reps = 0; reps < 10000; reps++) { for (array[0] = 0; array[0] < 10000; array[0]++) { output.clear(); input.clear(); encoder.encode(input, output, false); int len = output.position(); } }
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes()*height); imgIn.copyPixelsToBuffer(map); map.position(0); channel.close(); randomAccessFile.close();
CharsetDecoder cd = Charset.forName("UTF-8").newDecoder(); ByteBuffer in = ByteBuffer.wrap(bytes); CharBuffer out = CharBuffer.allocate(1); int p = 0; while (in.hasRemaining()) { cd.decode(in, out, true); char c = out.array()[0]; int nBytes = in.position() - p; p = in.position(); out.position(0); }