/** * Completely reads the given stream's bytes into a * {@code ByteString}, blocking if necessary until all bytes are * read through to the end of the stream. * * <b>Performance notes:</b> The returned {@code ByteString} is an * immutable tree of byte arrays ("chunks") of the stream data. The * first chunk is small, with subsequent chunks each being double * the size, up to 8K. If the caller knows the precise length of * the stream and wishes to avoid all unnecessary copies and * allocations, consider using the two-argument version of this * method, below. * * @param streamToDrain The source stream, which is read completely * but not closed. * @return A new {@code ByteString} which is made up of chunks of * various sizes, depending on the behavior of the underlying * stream. * @throws IOException IOException is thrown if there is a problem * reading the underlying stream. */ public static ByteString readFrom(InputStream streamToDrain) throws IOException { return readFrom( streamToDrain, MIN_READ_FROM_CHUNK_SIZE, MAX_READ_FROM_CHUNK_SIZE); }
/** * Completely reads the given stream's bytes into a * {@code ByteString}, blocking if necessary until all bytes are * read through to the end of the stream. * * <b>Performance notes:</b> The returned {@code ByteString} is an * immutable tree of byte arrays ("chunks") of the stream data. The * first chunk is small, with subsequent chunks each being double * the size, up to 8K. * * <p>Each byte read from the input stream will be copied twice to ensure * that the resulting ByteString is truly immutable. * * @param streamToDrain The source stream, which is read completely * but not closed. * @return A new {@code ByteString} which is made up of chunks of * various sizes, depending on the behavior of the underlying * stream. * @throws IOException IOException is thrown if there is a problem * reading the underlying stream. */ public static ByteString readFrom(InputStream streamToDrain) throws IOException { return readFrom(streamToDrain, MIN_READ_FROM_CHUNK_SIZE, MAX_READ_FROM_CHUNK_SIZE); }
/** * Completely reads the given stream's bytes into a * {@code ByteString}, blocking if necessary until all bytes are * read through to the end of the stream. * * <b>Performance notes:</b> The returned {@code ByteString} is an * immutable tree of byte arrays ("chunks") of the stream data. The * chunkSize parameter sets the size of these byte arrays. * * <p>Each byte read from the input stream will be copied twice to ensure * that the resulting ByteString is truly immutable. * * @param streamToDrain The source stream, which is read completely * but not closed. * @param chunkSize The size of the chunks in which to read the * stream. * @return A new {@code ByteString} which is made up of chunks of * the given size. * @throws IOException IOException is thrown if there is a problem * reading the underlying stream. */ public static ByteString readFrom(InputStream streamToDrain, int chunkSize) throws IOException { return readFrom(streamToDrain, chunkSize, chunkSize); }
/** * Completely reads the given stream's bytes into a * {@code ByteString}, blocking if necessary until all bytes are * read through to the end of the stream. * * <b>Performance notes:</b> The returned {@code ByteString} is an * immutable tree of byte arrays ("chunks") of the stream data. The * chunkSize parameter sets the size of these byte arrays. In * particular, if the chunkSize is precisely the same as the length * of the stream, unnecessary allocations and copies will be * avoided. Otherwise, the chunks will be of the given size, except * for the last chunk, which will be resized (via a reallocation and * copy) to contain the remainder of the stream. * * @param streamToDrain The source stream, which is read completely * but not closed. * @param chunkSize The size of the chunks in which to read the * stream. * @return A new {@code ByteString} which is made up of chunks of * the given size. * @throws IOException IOException is thrown if there is a problem * reading the underlying stream. */ public static ByteString readFrom(InputStream streamToDrain, int chunkSize) throws IOException { return readFrom(streamToDrain, chunkSize, chunkSize); }
/** * Creates a {@code ByteArray} object given an {@link InputStream}. The stream is read into the * created object. */ public static final ByteArray copyFrom(InputStream input) throws IOException { return new ByteArray(ByteString.readFrom(input)); }
/** * Creates a buffer with data read from a file. */ @SuppressWarnings("Finally") // Not concerned about suppression; expected to be exceedingly rare private ByteString createBufferFromFile(String fileClassPath) { ByteString buffer = ByteString.EMPTY; InputStream inputStream = getClass().getResourceAsStream(fileClassPath); if (inputStream == null) { throw new IllegalArgumentException("Unable to locate file on classpath: " + fileClassPath); } try { buffer = ByteString.readFrom(inputStream); } catch (IOException e) { throw new RuntimeException(e); } finally { try { inputStream.close(); } catch (IOException ignorable) { // ignore } } return buffer; }
ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
IOException { List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
List<AnnotateImageRequest> requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));