/** * Reset the buffer. * * @see Buffer#reset() * @param <T> the buffer type * @param buffer the buffer to reset * @return the buffer instance */ public static <T extends Buffer> T reset(T buffer) { buffer.reset(); return buffer; }
/** * 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(); }
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(); }
/** * Reset the buffer. * * @see Buffer#reset() * @param <T> the buffer type * @param buffer the buffer to reset * @return the buffer instance */ public static <T extends Buffer> T reset(T buffer) { buffer.reset(); return buffer; }
public Buffer reset() { ((java.nio.Buffer) buffer).reset(); return this; }
public Buffer reset( ) { return _buffer.reset( ); }
private void toReadMode(Buffer buffer) { buffer.limit(buffer.position()) .reset(); }
private void toReadMode(Buffer buffer) { buffer.limit(buffer.position()) .reset(); }
ByteBuffer buf = ByteBuffer.allocate(1024); // just allocate enough, can be reused later; buf.reset(); // in case this is reused, you need reset() SocketChannel channel = socket.getChannel(); channel.read(buf); short mId = buf.order(ByteOrder.LITTLE_ENDIAN).getShort(); byte[] otherBytes = new byte[....]; buf.get(otherBytes);
/** * 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(); }
/** * @see Buffer#reset(); */ public void reset() { getBuffer().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(); }
/** * @see Buffer#reset(); */ public void reset() { getBuffer().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())); }
protected static List<String> decodeStringSequence(ByteBuffer bb) { List<String> names = new ArrayList<String>(); bb.mark(); // first key starts from here while (bb.hasRemaining()) { if (bb.get() == 0) { ByteBuffer nameBuffer = (ByteBuffer) bb.duplicate().limit(bb.position() - 1).reset(); if (nameBuffer.hasRemaining()) { names.add(decodeString(nameBuffer)); } bb.mark(); // next key starts from here } } return names; }
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()));
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(); }