/** * Set the buffer mark. * * @see Buffer#mark() * @param <T> the buffer type * @param buffer the buffer to mark * @return the buffer instance */ public static <T extends Buffer> T mark(T buffer) { buffer.mark(); return buffer; }
/** * Fill with zeros the remaining of the supplied buffer. This method does * not change the buffer position. * <p> * Typically used for security reasons, with buffers that contains * now-unused plaintext. */ public void zeroRemaining() { ((Buffer) buffer).mark(); buffer.put(ZEROS, 0, buffer.remaining()); ((Buffer) buffer).reset(); }
/** * Fill the buffer with zeros. This method does not change the buffer position. * <p> * Typically used for security reasons, with buffers that contains * now-unused plaintext. */ public void zero() { ((Buffer) buffer).mark(); ((Buffer) buffer).position(0); buffer.put(ZEROS, 0, buffer.remaining()); ((Buffer) buffer).reset(); }
private static BomCharset detectCharsetFromBom(final ByteBuffer byteData) { final Buffer buffer = byteData; // .mark and rewind used to return Buffer, now ByteBuffer, so cast for backward compat buffer.mark(); byte[] bom = new byte[4]; if (byteData.remaining() >= bom.length) { byteData.get(bom); buffer.rewind(); } if (bom[0] == 0x00 && bom[1] == 0x00 && bom[2] == (byte) 0xFE && bom[3] == (byte) 0xFF || // BE bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE && bom[2] == 0x00 && bom[3] == 0x00) { // LE return new BomCharset("UTF-32", false); // and I hope it's on your system } else if (bom[0] == (byte) 0xFE && bom[1] == (byte) 0xFF || // BE bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE) { return new BomCharset("UTF-16", false); // in all Javas } else if (bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF) { return new BomCharset("UTF-8", true); // in all Javas // 16 and 32 decoders consume the BOM to determine be/le; utf-8 should be consumed here } return null; }
public String printBuffer(Buffer buf) { String info = buf.toString() + "< " + buf.position() + ", " + buf.mark() + ", " + buf.limit() + ", " + buf.capacity() + " >"; return info; } /*
Charset utf8 = Charset.forName("UTF-8"); CharsetEncoder encoder = utf8.newEncoder(); CharBuffer input = //allocate in some way, or pass as parameter ByteBuffer output = ByteBuffer.allocate(10); int limit = input.limit(); while(input.position() < limit) { output.clear(); input.mark(); input.limit(Math.max(input.position() + 2, input.capacity())); if (Character.isHighSurrogate(input.get()) && !Character.isLowSurrogate(input.get())) { //Malformed surrogate pair; do something! } input.limit(input.position()); input.reset(); encoder.encode(input, output, false); int encodedLen = output.position(); }
/** * Set the buffer mark. * * @see Buffer#mark() * @param <T> the buffer type * @param buffer the buffer to mark * @return the buffer instance */ public static <T extends Buffer> T mark(T buffer) { buffer.mark(); return buffer; }
public Buffer mark() { ((java.nio.Buffer) buffer).mark(); return this; }
public Buffer mark( ) { return _buffer.mark( ); }
private void toWriteMode(Buffer buffer) { buffer.mark() .position(buffer.limit()) .limit(buffer.capacity()); }
/** * Fill the buffer with zeros. This method does not change the buffer position. * <p> * Typically used for security reasons, with buffers that contains * now-unused plaintext. */ public void zero() { ((Buffer) buffer).mark(); ((Buffer) buffer).position(0); buffer.put(ZEROS, 0, buffer.remaining()); ((Buffer) buffer).reset(); }
/** * Fill with zeros the remaining of the supplied buffer. This method does * not change the buffer position. * <p> * Typically used for security reasons, with buffers that contains * now-unused plaintext. */ public void zeroRemaining() { ((Buffer) buffer).mark(); buffer.put(ZEROS, 0, buffer.remaining()); ((Buffer) buffer).reset(); }
public static String calculateEtag(final String s) throws java.security.NoSuchAlgorithmException { final java.nio.ByteBuffer buf = java.nio.charset.StandardCharsets.UTF_8.encode(s); final java.security.MessageDigest digest = java.security.MessageDigest.getInstance("SHA1"); buf.mark(); digest.update(buf); buf.reset(); return String.format("W/\"%s\"", javax.xml.bind.DatatypeConverter.printHexBinary(digest.digest())); }
private void toWriteMode(Buffer buffer) { buffer.mark() .position(buffer.limit()) .limit(buffer.capacity()); }
File file = new File("fileChannelTest.log"); FileOutputStream fos = new FileOutputStream(file); FileChannel fileChannel = fos.getChannel(); ByteBuffer bb1 = ByteBuffer.wrap("This is a log line to test!\n".getBytes()); ByteBuffer bb2 = ByteBuffer.allocateDirect(bb1.remaining()); bb2.put(bb1).flip(); bb2.mark(); long freeMemory = Runtime.getRuntime().freeMemory(); for (int i = 0; i < 1000000; i++) { bb2.reset(); fileChannel.write(bb2); } System.out.println("Memory allocated: " + (freeMemory - Runtime.getRuntime().freeMemory()));
void findPairs(Mat objectDescriptors, Mat imageDescriptors) { int size = imageDescriptors.cols(); ByteBuffer objectBuf = objectDescriptors.createBuffer(); ByteBuffer imageBuf = imageDescriptors.createBuffer(); for (int i = 0; i * size < objectBuf.capacity(); i++) { ByteBuffer descriptor = (ByteBuffer)objectBuf.position(i * size).limit((i + 1) * size).mark(); int nearestNeighbor = naiveNearestNeighbor(descriptor, imageBuf); if (nearestNeighbor >= 0) { ptpairs.add(i); ptpairs.add(nearestNeighbor); } } }
void findPairs(Mat objectDescriptors, Mat imageDescriptors) { int size = imageDescriptors.cols(); ByteBuffer objectBuf = objectDescriptors.createBuffer(); ByteBuffer imageBuf = imageDescriptors.createBuffer(); for (int i = 0; i * size < objectBuf.capacity(); i++) { ByteBuffer descriptor = (ByteBuffer) objectBuf.position(i * size).limit((i + 1) * size).mark(); int nearestNeighbor = naiveNearestNeighbor(descriptor, imageBuf); if (nearestNeighbor >= 0) { ptpairs.add(i); ptpairs.add(nearestNeighbor); } } }
ByteBuffer buffer = ByteBuffer.allocate(Resource.MEMORY_ALLOC_SIZE); while (fic.read(buffer) >= 0) { buffer.flip(); buffer.mark(); while (buffer.hasRemaining()) { channel2.write(buffer); } buffer.reset(): while (buffer.hasRemaining()) { channel.write(buffer); } buffer.clear(); }