/** * Attempts to read {@code len} bytes from the stream into the given array starting at {@code * off}, with the same behavior as {@link DataInput#readFully(byte[], int, int)}. Does not close * the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is read. * @param off an int specifying the offset into the data. * @param len an int specifying the number of bytes to read. * @throws EOFException if this stream reaches the end before reading all the bytes. * @throws IOException if an I/O error occurs. */ public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException { int read = read(in, b, off, len); if (read != len) { throw new EOFException( "reached end of stream after reading " + read + " bytes; " + len + " bytes expected"); } }
public LogMessage nextMessage() throws IOException { stream.mark(HEADER_SIZE); // Read header final byte[] headerBytes = new byte[HEADER_SIZE]; final int n = ByteStreams.read(stream, headerBytes, 0, HEADER_SIZE); if (n == 0) { return null; } final ByteBuffer header = ByteBuffer.wrap(headerBytes); int streamId = header.get(); final int idZ = header.getInt(0); // Read frame final byte[] frame; // Header format is : {STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4} if (idZ == 0 || idZ == 0x01000000 || idZ == 0x02000000) { header.position(FRAME_SIZE_OFFSET); final int frameSize = header.getInt(); frame = new byte[frameSize]; } else { stream.reset(); streamId = Stream.STDOUT.id(); frame = new byte[stream.available()]; } ByteStreams.readFully(stream, frame); return new LogMessage(streamId, ByteBuffer.wrap(frame)); }
/** * Attempts to read {@code len} bytes from the stream into the given array starting at {@code * off}, with the same behavior as {@link DataInput#readFully(byte[], int, int)}. Does not close * the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is read. * @param off an int specifying the offset into the data. * @param len an int specifying the number of bytes to read. * @throws EOFException if this stream reaches the end before reading all the bytes. * @throws IOException if an I/O error occurs. */ public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException { int read = read(in, b, off, len); if (read != len) { throw new EOFException( "reached end of stream after reading " + read + " bytes; " + len + " bytes expected"); } }
/** * Attempts to read {@code len} bytes from the stream into the given array starting at {@code * off}, with the same behavior as {@link DataInput#readFully(byte[], int, int)}. Does not close * the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is read. * @param off an int specifying the offset into the data. * @param len an int specifying the number of bytes to read. * @throws EOFException if this stream reaches the end before reading all the bytes. * @throws IOException if an I/O error occurs. */ public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException { int read = read(in, b, off, len); if (read != len) { throw new EOFException( "reached end of stream after reading " + read + " bytes; " + len + " bytes expected"); } }
/** * Checks that the contents of this byte source are equal to the contents of the given byte * source. * * @throws IOException if an I/O error occurs while reading from this source or {@code other} */ public boolean contentEquals(ByteSource other) throws IOException { checkNotNull(other); byte[] buf1 = createBuffer(); byte[] buf2 = createBuffer(); Closer closer = Closer.create(); try { InputStream in1 = closer.register(openStream()); InputStream in2 = closer.register(other.openStream()); while (true) { int read1 = ByteStreams.read(in1, buf1, 0, buf1.length); int read2 = ByteStreams.read(in2, buf2, 0, buf2.length); if (read1 != read2 || !Arrays.equals(buf1, buf2)) { return false; } else if (read1 != buf1.length) { return true; } } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * This is a copy of ByteStreams.readFully(), with the slight change that it throws * NoBytesReadException if zero bytes are read. Otherwise it is identical. * * @param in * @param bytes * @throws IOException */ private void readFully(InputStream in, byte[] bytes) throws IOException { int read = ByteStreams.read(in, bytes, 0, bytes.length); if (read == 0) { throw new NoBytesReadException(); } else if (read != bytes.length) { throw new EOFException("reached end of stream after reading " + read + " bytes; " + bytes.length + " bytes expected"); } }
/** * Checks that the contents of this byte source are equal to the contents of the given byte * source. * * @throws IOException if an I/O error occurs while reading from this source or {@code other} */ public boolean contentEquals(ByteSource other) throws IOException { checkNotNull(other); byte[] buf1 = createBuffer(); byte[] buf2 = createBuffer(); Closer closer = Closer.create(); try { InputStream in1 = closer.register(openStream()); InputStream in2 = closer.register(other.openStream()); while (true) { int read1 = ByteStreams.read(in1, buf1, 0, buf1.length); int read2 = ByteStreams.read(in2, buf2, 0, buf2.length); if (read1 != read2 || !Arrays.equals(buf1, buf2)) { return false; } else if (read1 != buf1.length) { return true; } } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Checks that the contents of this byte source are equal to the contents of the given byte * source. * * @throws IOException if an I/O error occurs while reading from this source or {@code other} */ public boolean contentEquals(ByteSource other) throws IOException { checkNotNull(other); byte[] buf1 = createBuffer(); byte[] buf2 = createBuffer(); Closer closer = Closer.create(); try { InputStream in1 = closer.register(openStream()); InputStream in2 = closer.register(other.openStream()); while (true) { int read1 = ByteStreams.read(in1, buf1, 0, buf1.length); int read2 = ByteStreams.read(in2, buf2, 0, buf2.length); if (read1 != read2 || !Arrays.equals(buf1, buf2)) { return false; } else if (read1 != buf1.length) { return true; } } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
private Message decodeMessage(ChannelHandlerContext ctx, List<Frame> frames) throws IOException { long frameType = frames.get(0).getType(); byte[] payload = new byte[frames.size() == 1 ? frames.get(0).getSize() : frames.get(0).totalFrameSize]; int pos = 0; for (Frame frame : frames) { pos += ByteStreams.read(frame.getStream(), payload, pos, frame.getSize()); } if (loggerWire.isDebugEnabled()) loggerWire.debug("Recv: Encoded: {} [{}]", frameType, toHexString(payload)); Message msg; try { msg = createMessage((byte) frameType, payload); if (loggerNet.isDebugEnabled()) loggerNet.debug("From: {} Recv: {}", channel, msg.toString()); } catch (Exception ex) { loggerNet.debug(String.format("Incorrectly encoded message from: \t%s, dropping peer", channel), ex); channel.disconnect(ReasonCode.BAD_PROTOCOL); return null; } ethereumListener.onRecvMessage(channel, msg); channel.getNodeStatistics().rlpxInMessages.add(); return msg; }
@Override public void send(final InputStream stream) throws Exception { byte[] chunk = new byte[bufferSize]; int count = ByteStreams.read(stream, chunk, 0, bufferSize); if (count <= 0) { return;
while (remainingData > 0) { int nextBlock = (int) Math.min(remainingData, 1024); ByteStreams.read(stream, data, 0, nextBlock); channelHandlerContext.getChannel().write( NetData.NetMessage.newBuilder().setModuleData(
private void verifyStreamContents(byte[] uncompressedData) throws IOException { byte[] decompressedData = new byte[uncompressedData.length]; ByteStreams.read(decompressingStream, decompressedData, 0, decompressedData.length); assertArrayEquals(uncompressedData, decompressedData); assertStreamIsEmpty(decompressingStream); }
private void storeBlocksFromStream(@Nullable Transaction tx, long id, InputStream stream) throws IOException { long blockNumber = 0; while (true) { byte[] bytesToStore = new byte[BLOCK_SIZE_IN_BYTES]; int length = ByteStreams.read(stream, bytesToStore, 0, BLOCK_SIZE_IN_BYTES); // Store only relevant data if it only filled a partial block if (length == 0) { break; } if (length < BLOCK_SIZE_IN_BYTES) { // This is the last block. storeBlockWithNonNullTransaction(tx, id, blockNumber, PtBytes.head(bytesToStore, length)); break; } else { // Store a full block. storeBlockWithNonNullTransaction(tx, id, blockNumber, bytesToStore); } blockNumber++; if (!streamOperationIsTransactional(tx)) { backoffStrategy.accept(blockNumber); } } }
@Override protected int refill() throws IOException { if (finished) { return 0; } writeBufferPosition = 0; // Read a block of data from the delegate input stream. int bytesRead = ByteStreams.read(delegate, uncompressedBuffer, BUFFER_START, blockSize); if (bytesRead == 0) { // The delegate input stream has been exhausted, so we notify // the compressing stream that there are no remaining bytes. // Lastly, we flush the LZ4 footer into the internal buffer. compressingStream.finish(); compressingStream.flush(); finished = true; } else { // Write the bytes to the compressing stream and flush it. compressingStream.write(uncompressedBuffer, BUFFER_START, bytesRead); compressingStream.flush(); } return writeBufferPosition; }
@Test public void testMultiBlock_readPastEnd() throws IOException { byte[] uncompressedData = new byte[16 * BLOCK_SIZE]; fillWithCompressibleData(uncompressedData); initializeStreams(uncompressedData); byte[] decompressedData = new byte[17 * BLOCK_SIZE]; int bytesRead = ByteStreams.read(decompressingStream, decompressedData, 0, decompressedData.length); assertEquals(uncompressedData.length, bytesRead); assertArrayEquals(uncompressedData, Arrays.copyOf(decompressedData, bytesRead)); }
/** * Attempts to fill the supplied buffer from an input stream, reseting the input stream back to it's original * position. Returns the actual number of bytes read. */ private static int readAndReset(InputStream in, byte[] buffer) throws IOException { in.mark(buffer.length); try { return ByteStreams.read(in, buffer, 0, buffer.length); } finally { in.reset(); } }
/** * Returns a new {@code SaneWord} by consuming {@link #SIZE_IN_BYTES} bytes from the given * {@link InputStream}. */ public static SaneWord fromStream(InputStream input) throws IOException { byte[] newValue = new byte[SIZE_IN_BYTES]; if (ByteStreams.read(input, newValue, 0, newValue.length) != newValue.length) { throw new IOException("input stream was truncated while reading a word"); } return new SaneWord(newValue); }
@Override public int read(long position, byte[] buffer, int offset, int length) throws IOException { try (InputStream in = _stashReader.getRawSplitPart(_stashSplit, Range.closedOpen(position, position + length))) { return ByteStreams.read(in, buffer, offset, length); } }
@Override public int read(long position, byte[] buffer, int offset, int length) throws IOException { try (InputStream in = _stashReader.getRawSplitPart(_stashSplit, Range.closedOpen(position, position + length))) { return ByteStreams.read(in, buffer, offset, length); } }
@Override public long getSize(@NotNull GitObject<? extends ObjectId> objectId) throws IOException { final ObjectLoader loader = objectId.openObject(); final ObjectStream stream = loader.openStream(); final byte[] header = new byte[Constants.POINTER_MAX_SIZE]; int length = ByteStreams.read(stream, header, 0, header.length); if (length < header.length) { final Map<String, String> pointer = Pointer.parsePointer(header, 0, length); if (pointer != null) { return getReader(pointer).getSize(); } } return loader.getSize(); }