Java Code Examples for com.google.common.io.ByteStreams#readFully()
The following examples show how to use
com.google.common.io.ByteStreams#readFully() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileCacheReadingWriter.java From BIMserver with GNU Affero General Public License v3.0 | 6 votes |
@Override public boolean writeMessage(OutputStream outputStream, ProgressReporter progressReporter) throws IOException, SerializerException { if (outputStream instanceof ReusableLittleEndianDataOutputStream) { ReusableLittleEndianDataOutputStream reusableLittleEndianDataOutputStream = (ReusableLittleEndianDataOutputStream)outputStream; reusableLittleEndianDataOutputStream.ensureExtraCapacity(nextSize); GrowingByteBuffer growingByteBuffer = reusableLittleEndianDataOutputStream.getGrowingByteBuffer(); ByteBuffer targetBuffer = growingByteBuffer.getByteBuffer(); inputStream.readFully(targetBuffer.array(), targetBuffer.position(), nextSize); targetBuffer.position(targetBuffer.position() + nextSize); nextSize = inputStream.readInt(); return nextSize != -1; } else { if (buffer == null || nextSize > buffer.length) { buffer = new byte[nextSize]; } ByteStreams.readFully(inputStream, buffer, 0, nextSize); outputStream.write(buffer, 0, nextSize); nextSize = inputStream.readInt(); return nextSize != -1; } }
Example 2
Source File: DexBackedDexFile.java From HeyGirl with Apache License 2.0 | 6 votes |
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is) throws IOException { if (!is.markSupported()) { throw new IllegalArgumentException("InputStream must support mark"); } is.mark(44); byte[] partialHeader = new byte[44]; try { ByteStreams.readFully(is, partialHeader); } catch (EOFException ex) { throw new NotADexFile("File is too short"); } finally { is.reset(); } verifyMagicAndByteOrder(partialHeader, 0); byte[] buf = ByteStreams.toByteArray(is); return new DexBackedDexFile(opcodes, buf, 0, false); }
Example 3
Source File: PersistTachyon.java From h2o-2 with Apache License 2.0 | 6 votes |
@Override public byte[] load(Value v) { Key k = v._key; // key for value if (k._kb[0] != Key.DVEC) throw H2O.unimpl(); // Load only from values stored in vector long skip = FileVec.chunkOffset(k); // Compute skip for this value long start_io_ms = System.currentTimeMillis(); final byte[] b = MemoryManager.malloc1(v._max); String[] keyComp = decodeKey(k); String clientUri = keyComp[0]; String fpath = keyComp[1]; TachyonFS tfs = null; InputStream is = null; try { tfs = (TachyonFS) (Persist.I[Value.TACHYON].createClient(clientUri)); long start_ns = System.nanoTime(); // Blocking i/o call timing - without counting repeats is = tfs.getFile(fpath).getInStream(ReadType.NO_CACHE); ByteStreams.skipFully(is, skip); ByteStreams.readFully(is, b); TimeLine.record_IOclose(start_ns, start_io_ms, 1/* read */, v._max, Value.TACHYON); return b; } catch (IOException e) { throw new RuntimeException(Log.err("File load failed: ", e)); } finally { if (is!=null) Utils.close(is); } }
Example 4
Source File: ClientConnectionHandler.java From bboxdb with Apache License 2.0 | 6 votes |
/** * Read the full package. The total length of the package is read from the package header. * @param packageHeader * @return * @throws IOException */ private ByteBuffer readFullPackage(final ByteBuffer packageHeader, final InputStream inputStream) throws IOException { final int bodyLength = (int) NetworkPackageDecoder.getBodyLengthFromRequestPackage(packageHeader); final int headerLength = packageHeader.limit(); final int packageLength = headerLength + bodyLength; final ByteBuffer encodedPackage = ByteBuffer.allocate(packageLength); try { //System.out.println("Trying to read: " + bodyLength + " avail " + in.available()); encodedPackage.put(packageHeader.array()); ByteStreams.readFully(inputStream, encodedPackage.array(), encodedPackage.position(), bodyLength); readBytesCounter.inc(packageLength); } catch (IOException e) { serviceState.dispatchToStopping(); throw e; } return encodedPackage; }
Example 5
Source File: ProtoClient.java From java with Apache License 2.0 | 5 votes |
private Unknown parse(InputStream stream) throws ApiException, IOException { byte[] magic = new byte[4]; ByteStreams.readFully(stream, magic); if (!Arrays.equals(magic, MAGIC)) { throw new ApiException("Unexpected magic number: " + Hex.encodeHexString(magic)); } return Unknown.parseFrom(stream); }
Example 6
Source File: UnsafeSorterSpillReader.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@Override public void loadNext() throws IOException { recordLength = din.readInt(); keyPrefix = din.readLong(); if (recordLength > arr.length) { arr = new byte[recordLength]; baseObject = arr; } ByteStreams.readFully(in, arr, 0, recordLength); numRecordsRemaining--; if (numRecordsRemaining == 0) { close(); } }
Example 7
Source File: HttpClientIntegrationTest.java From armeria with Apache License 2.0 | 5 votes |
private static void testSocketOutput(String path, IntFunction<String> expectedResponse) throws IOException { Socket s = null; try (ServerSocket ss = new ServerSocket(0)) { final int port = ss.getLocalPort(); final String expected = expectedResponse.apply(port); // Send a request. Note that we do not wait for a response anywhere because we are only interested // in testing what client sends. WebClient.builder("none+h1c://127.0.0.1:" + port) .factory(clientFactory) .build() .get(path); ss.setSoTimeout(10000); s = ss.accept(); final byte[] buf = new byte[expected.length()]; final InputStream in = s.getInputStream(); // Read the encoded request. s.setSoTimeout(10000); ByteStreams.readFully(in, buf); // Ensure that the encoded request matches. assertThat(new String(buf, StandardCharsets.US_ASCII)).isEqualTo(expected); // Should not send anything more. s.setSoTimeout(1000); assertThatThrownBy(in::read).isInstanceOf(SocketTimeoutException.class); } finally { Closeables.close(s, true); } }
Example 8
Source File: DexBackedOdexFile.java From zjdroid with Apache License 2.0 | 5 votes |
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is) throws IOException { if (!is.markSupported()) { throw new IllegalArgumentException("InputStream must support mark"); } is.mark(8); byte[] partialHeader = new byte[8]; try { ByteStreams.readFully(is, partialHeader); } catch (EOFException ex) { throw new NotADexFile("File is too short"); } finally { is.reset(); } verifyMagic(partialHeader); is.reset(); byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE]; ByteStreams.readFully(is, odexBuf); int dexOffset = OdexHeaderItem.getDexOffset(odexBuf); if (dexOffset > OdexHeaderItem.ITEM_SIZE) { ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE); } byte[] dexBuf = ByteStreams.toByteArray(is); return new DexBackedOdexFile(opcodes, odexBuf, dexBuf); }
Example 9
Source File: DexBackedOdexFile.java From AppTroy with Apache License 2.0 | 5 votes |
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is) throws IOException { if (!is.markSupported()) { throw new IllegalArgumentException("InputStream must support mark"); } is.mark(8); byte[] partialHeader = new byte[8]; try { ByteStreams.readFully(is, partialHeader); } catch (EOFException ex) { throw new NotADexFile("File is too short"); } finally { is.reset(); } verifyMagic(partialHeader); is.reset(); byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE]; ByteStreams.readFully(is, odexBuf); int dexOffset = OdexHeaderItem.getDexOffset(odexBuf); if (dexOffset > OdexHeaderItem.ITEM_SIZE) { ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE); } byte[] dexBuf = ByteStreams.toByteArray(is); return new DexBackedOdexFile(opcodes, odexBuf, dexBuf); }
Example 10
Source File: LogReader.java From docker-client with Apache License 2.0 | 5 votes |
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)); }
Example 11
Source File: CountingLittleEndianDataInputStream.java From BIMserver with GNU Affero General Public License v3.0 | 4 votes |
@Override public void readFully(byte[] b, int off, int len) throws IOException { ByteStreams.readFully(this, b, off, len); pos += len; }
Example 12
Source File: NettyTestUtil.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
static String toString(InputStream in) throws Exception { byte[] bytes = new byte[in.available()]; ByteStreams.readFully(in, bytes); return new String(bytes, UTF_8); }
Example 13
Source File: Streams.java From imhotep with Apache License 2.0 | 4 votes |
public static int readInt(final InputStream is) throws IOException { final byte[] buf = new byte[4]; ByteStreams.readFully(is, buf); return Bytes.bytesToInt(buf); }
Example 14
Source File: ServerResponseReader.java From bboxdb with Apache License 2.0 | 4 votes |
/** * Read the next response package header from the server * @return * @throws IOException */ protected ByteBuffer readNextResponsePackageHeader(final InputStream inputStream) throws IOException { final ByteBuffer bb = ByteBuffer.allocate(12); ByteStreams.readFully(inputStream, bb.array(), 0, bb.limit()); return bb; }
Example 15
Source File: BlockAwareSegmentInputStreamTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testNoEntryPutIn() throws Exception { // simulate first entry size over the block size budget, it shouldn't be added. // 2 entries, each with bigger size than block size, so there should no entry added into block. int ledgerId = 1; int entrySize = 1000; int lac = 1; ReadHandle readHandle = new MockReadHandle(ledgerId, entrySize, lac); // set block size not able to hold one entry int blockSize = DataBlockHeaderImpl.getDataStartOffset() + entrySize; BlockAwareSegmentInputStreamImpl inputStream = new BlockAwareSegmentInputStreamImpl(readHandle, 0, blockSize); int expectedEntryCount = 0; // verify get methods assertEquals(inputStream.getLedger(), readHandle); assertEquals(inputStream.getStartEntryId(), 0); assertEquals(inputStream.getBlockSize(), blockSize); // verify read inputStream // 1. read header. 128 byte headerB[] = new byte[DataBlockHeaderImpl.getDataStartOffset()]; ByteStreams.readFully(inputStream, headerB); DataBlockHeader headerRead = DataBlockHeaderImpl.fromStream(new ByteArrayInputStream(headerB)); assertEquals(headerRead.getBlockLength(), blockSize); assertEquals(headerRead.getFirstEntryId(), 0); // 2. since no entry put in, it should only get padding after header. byte padding[] = new byte[blockSize - DataBlockHeaderImpl.getDataStartOffset()]; inputStream.read(padding); ByteBuf paddingBuf = Unpooled.wrappedBuffer(padding); IntStream.range(0, paddingBuf.capacity()/4).forEach(i -> assertEquals(Integer.toHexString(paddingBuf.readInt()), Integer.toHexString(0xFEDCDEAD))); // 3. reach end. assertEquals(inputStream.read(), -1); assertEquals(inputStream.getBlockEntryCount(), 0); assertEquals(inputStream.getBlockEntryBytesCount(), 0); assertEquals(inputStream.getEndEntryId(), -1); inputStream.close(); }
Example 16
Source File: Streams.java From imhotep with Apache License 2.0 | 4 votes |
public static long readLong(final InputStream is) throws IOException { final byte[] buf = new byte[8]; ByteStreams.readFully(is, buf); return Bytes.bytesToLong(buf); }
Example 17
Source File: LittleEndianDataInputStream.java From LevelDb2Avnil with GNU General Public License v2.0 | 4 votes |
@Override public void readFully(byte[] b) throws IOException { ByteStreams.readFully(this, b); }
Example 18
Source File: Streams.java From imhotep with Apache License 2.0 | 4 votes |
public static String readUTF8String(final InputStream is, final int len) throws IOException { final byte[] bytes = new byte[len]; ByteStreams.readFully(is, bytes); return new String(bytes, Charsets.UTF_8); }
Example 19
Source File: NettyTestUtil.java From grpc-java with Apache License 2.0 | 4 votes |
static String toString(InputStream in) throws Exception { byte[] bytes = new byte[in.available()]; ByteStreams.readFully(in, bytes); return new String(bytes, UTF_8); }
Example 20
Source File: DataEncoderHelper.java From bboxdb with Apache License 2.0 | 2 votes |
/** * Read an integer from a stream * @param inputStream * @return * @throws IOException */ public static int readIntFromStream(final InputStream inputStream) throws IOException { final byte[] elementBytes = new byte[DataEncoderHelper.INT_BYTES]; ByteStreams.readFully(inputStream, elementBytes, 0, elementBytes.length); return DataEncoderHelper.readIntFromByte(elementBytes); }