/** * Attempts to read enough bytes from the stream to fill the given byte array, with the same * behavior as {@link DataInput#readFully(byte[])}. Does not close the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is 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) throws IOException { readFully(in, b, 0, b.length); }
@Override public void readFully(byte[] b) throws IOException { ByteStreams.readFully(this, b); }
@Override public void readFully(byte[] b, int off, int len) throws IOException { ByteStreams.readFully(this, b, off, len); }
/** * Attempts to read enough bytes from the stream to fill the given byte array, with the same * behavior as {@link DataInput#readFully(byte[])}. Does not close the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is 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) throws IOException { readFully(in, b, 0, b.length); }
@Override public void readFully(byte[] b) throws IOException { ByteStreams.readFully(this, b); }
@Override public void readFully(byte[] b, int off, int len) throws IOException { ByteStreams.readFully(this, b, off, len); }
/** * Attempts to read enough bytes from the stream to fill the given byte array, with the same * behavior as {@link DataInput#readFully(byte[])}. Does not close the stream. * * @param in the input stream to read from. * @param b the buffer into which the data is 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) throws IOException { readFully(in, b, 0, b.length); }
@Override public void readFully(byte[] b) throws IOException { ByteStreams.readFully(this, b); }
@Override public void readFully(byte[] b, int off, int len) throws IOException { ByteStreams.readFully(this, b, off, len); }
private int readInt(InputStream in) throws IOException { byte[] intBytes = new byte[Integer.BYTES]; ByteStreams.readFully(in, intBytes); return Ints.fromByteArray(intBytes); }
long readLong(InputStream in) throws IOException { byte[] longBytes = new byte[Long.BYTES]; ByteStreams.readFully(in, longBytes); return Longs.fromByteArray(longBytes); }
String readString(InputStream in) throws IOException { final int length = readInt(in); byte[] stringBytes = new byte[length]; ByteStreams.readFully(in, stringBytes); return StringUtils.fromUtf8(stringBytes); }
@Nonnull public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is) throws IOException { DexUtil.verifyOdexHeader(is); 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); }
@Override public Object call() throws Exception { FileSystem fs = FileSystem.get(p.toUri(), CONF); FSDataInputStream s = null; try { s = fs.open(p); // NOTE: // The following line degrades performance of HDFS load from S3 API: s.readFully(skip,b,0,b.length); // Google API's simple seek has better performance // Load of 300MB file via Google API ~ 14sec, via s.readFully ~ 5min (under the same condition) ByteStreams.skipFully(s, skip_); ByteStreams.readFully(s, b); assert v.isPersisted(); } finally { Utils.close(s); } return null; } }, true, v._max);
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)); }
ByteStreams.readFully(is, payload.array()); } catch (EOFException e) { throw new IOException("Failed to read log message. Could not read all " + size + " bytes. " + e.getMessage() +
ByteStreams.readFully(newTestStream(10), null, 0, 10); fail("expected exception"); } catch (NullPointerException e) { ByteStreams.readFully(null, b, 0, 10); fail("expected exception"); } catch (NullPointerException e) { ByteStreams.readFully(newTestStream(10), b, -1, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 2, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(5), b, 0, 10); fail("expected exception"); } catch (EOFException e) { ByteStreams.readFully(newTestStream(10), b, 0, 0);
long start_ns = System.nanoTime(); // Blocking i/o call timing - without counting repeats s = getObjectForKey(k, skip, v._max).getObjectContent(); ByteStreams.readFully(s, b); // delegate work to Google (it reads the byte buffer in a cycle as we did) assert v.isPersisted(); TimeLine.record_IOclose(start_ns, start_io_ms, 1/* read */, v._max, Value.S3);
@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); } }
@Override public void readFully(byte[] b) throws IOException { ByteStreams.readFully(this, b); pos += b.length; }