@Override protected void processRemaining(ByteBuffer buffer) { b += buffer.remaining(); for (int i = 0; buffer.hasRemaining(); i += 8) { finalM ^= (buffer.get() & 0xFFL) << i; } }
/** * Initialize an instance based upon the underlying storage from {@code value}. * There is a potential to share the underlying array storage if {@link ByteBuffer#hasArray()} is {@code true}. * if {@code copy} is {@code true} a copy will be made of the memory. * if {@code copy} is {@code false} the underlying storage will be shared, if possible. */ public AsciiString(ByteBuffer value, boolean copy) { this(value, value.position(), value.remaining(), copy); }
/** * transfers appReadBuffer contents (decrypted data) into dst bytebuffer * @param dst ByteBuffer */ private int readFromAppBuffer(ByteBuffer dst) { appReadBuffer.flip(); int remaining = Math.min(appReadBuffer.remaining(), dst.remaining()); if (remaining > 0) { int limit = appReadBuffer.limit(); appReadBuffer.limit(appReadBuffer.position() + remaining); dst.put(appReadBuffer); appReadBuffer.limit(limit); } appReadBuffer.compact(); return remaining; }
static ByteBuffer encodeChunk(ByteBuffer data) { if (data.remaining() == 0) { return ByteBuffer.wrap(LAST_CHUNK); } byte[] startBytes = getChunkHeaderBytes(data.remaining()); ByteBuffer chunkBuffer = ByteBuffer.allocate(startBytes.length + data.remaining() + 2); chunkBuffer.put(startBytes); chunkBuffer.put(data); chunkBuffer.put(LINE_SEPARATOR_BYTES); chunkBuffer.flip(); return chunkBuffer; }
/** * Returns the checksum of all but the first 12 bytes of {@code dex}. */ public int computeChecksum() throws IOException { Adler32 adler32 = new Adler32(); byte[] buffer = new byte[8192]; ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe data.limit(data.capacity()); data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM); while (data.hasRemaining()) { int count = Math.min(buffer.length, data.remaining()); data.get(buffer, 0, count); adler32.update(buffer, 0, count); } return (int) adler32.getValue(); }
private Hasher putBytesInternal(ByteBuffer readBuffer) { // If we have room for all of it, this is easy if (readBuffer.remaining() <= buffer.remaining()) { buffer.put(readBuffer); munchIfFull(); return this; } // First add just enough to fill buffer size, and munch that int bytesToCopy = bufferSize - buffer.position(); for (int i = 0; i < bytesToCopy; i++) { buffer.put(readBuffer.get()); } munch(); // buffer becomes empty here, since chunkSize divides bufferSize // Now process directly from the rest of the input buffer while (readBuffer.remaining() >= chunkSize) { process(readBuffer); } // Finally stick the remainder back in our usual buffer buffer.put(readBuffer); return this; }
@Override public Hasher putBytes(ByteBuffer buffer) { ByteOrder bo = buffer.order(); buffer.order(ByteOrder.LITTLE_ENDIAN); while (buffer.remaining() >= 4) { putInt(buffer.getInt()); } while (buffer.hasRemaining()) { putByte(buffer.get()); } buffer.order(bo); return this; }
@Override public Object read(ByteBuffer buffer) { int size = buffer.getInt(); if (size < 0) return null; if (size > buffer.remaining()) throw new SchemaException("Error reading bytes of size " + size + ", only " + buffer.remaining() + " bytes available"); ByteBuffer val = buffer.slice(); val.limit(size); buffer.position(buffer.position() + size); return val; }
/** * Try to read an EOL incrementing the buffer position if successful. * @return whether an EOL was consumed */ private boolean tryConsumeEndOfLine(ByteBuffer byteBuffer) { if (byteBuffer.remaining() > 0) { byte b = byteBuffer.get(); if (b == '\n') { return true; } else if (b == '\r') { if (byteBuffer.remaining() > 0 && byteBuffer.get() == '\n') { return true; } else { throw new StompConversionException("'\\r' must be followed by '\\n'"); } } // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer ((Buffer) byteBuffer).position(byteBuffer.position() - 1); } return false; }
private static BufferOrEvent generateAndWriteEvent(FileChannel fileChannel, Random rnd, int numberOfChannels) throws IOException { long magicNumber = rnd.nextLong(); byte[] data = new byte[rnd.nextInt(1000)]; rnd.nextBytes(data); TestEvent evt = new TestEvent(magicNumber, data); int channelIndex = rnd.nextInt(numberOfChannels); ByteBuffer serializedEvent = EventSerializer.toSerializedEvent(evt); ByteBuffer header = ByteBuffer.allocate(9); header.order(ByteOrder.LITTLE_ENDIAN); header.putInt(channelIndex); header.putInt(serializedEvent.remaining()); header.put((byte) 1); header.flip(); FileUtils.writeCompletely(fileChannel, header); FileUtils.writeCompletely(fileChannel, serializedEvent); return new BufferOrEvent(evt, channelIndex); }
@BeforeClass public static void setUp() { final ByteBuffer buffer = ByteBuffer.wrap(allBytes); while (buffer.hasRemaining()) { final byte[] bytes = new byte[Math.min(RANDOM.nextInt(128), buffer.remaining())]; RANDOM.nextBytes(bytes); buffer.put(bytes); BYTE_LIST.add(bytes); } }
@Test public void writeWritableByteChannelErrorInWrite() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); WritableByteChannel channel = mock(WritableByteChannel.class); when(channel.write(any())) .thenAnswer(invocation -> { ByteBuffer buffer = invocation.getArgument(0); int written = buffer.remaining(); buffer.position(buffer.limit()); return written; }) .thenThrow(new IOException()); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError(IOException.class) .verify(Duration.ofSeconds(3)); channel.close(); }
ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) { super(buffer.remaining()); if (!buffer.isReadOnly()) { throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer)); } this.allocator = allocator; this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN); writerIndex(this.buffer.limit()); }
/** Updates this hasher with bytes from the given buffer. */ protected void update(ByteBuffer b) { if (b.hasArray()) { update(b.array(), b.arrayOffset() + b.position(), b.remaining()); b.position(b.limit()); } else { for (int remaining = b.remaining(); remaining > 0; remaining--) { update(b.get()); } } }