001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.io; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019import static com.google.common.base.Preconditions.checkPositionIndex; 020import static com.google.common.base.Preconditions.checkPositionIndexes; 021import static java.lang.Math.max; 022import static java.lang.Math.min; 023 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.annotations.J2ktIncompatible; 026import com.google.common.math.IntMath; 027import com.google.errorprone.annotations.CanIgnoreReturnValue; 028import java.io.ByteArrayInputStream; 029import java.io.ByteArrayOutputStream; 030import java.io.DataInput; 031import java.io.DataInputStream; 032import java.io.DataOutput; 033import java.io.DataOutputStream; 034import java.io.EOFException; 035import java.io.FilterInputStream; 036import java.io.IOException; 037import java.io.InputStream; 038import java.io.OutputStream; 039import java.nio.ByteBuffer; 040import java.nio.channels.FileChannel; 041import java.nio.channels.ReadableByteChannel; 042import java.nio.channels.WritableByteChannel; 043import java.util.ArrayDeque; 044import java.util.Arrays; 045import java.util.Queue; 046import org.jspecify.annotations.Nullable; 047 048/** 049 * Provides utility methods for working with byte arrays and I/O streams. 050 * 051 * @author Chris Nokleberg 052 * @author Colin Decker 053 * @since 1.0 054 */ 055@GwtIncompatible 056public final class ByteStreams { 057 058 private static final int BUFFER_SIZE = 8192; 059 060 /** Creates a new byte array for buffering reads or writes. */ 061 static byte[] createBuffer() { 062 return new byte[BUFFER_SIZE]; 063 } 064 065 /** 066 * There are three methods to implement {@link FileChannel#transferTo(long, long, 067 * WritableByteChannel)}: 068 * 069 * <ol> 070 * <li>Use sendfile(2) or equivalent. Requires that both the input channel and the output 071 * channel have their own file descriptors. Generally this only happens when both channels 072 * are files or sockets. This performs zero copies - the bytes never enter userspace. 073 * <li>Use mmap(2) or equivalent. Requires that either the input channel or the output channel 074 * have file descriptors. Bytes are copied from the file into a kernel buffer, then directly 075 * into the other buffer (userspace). Note that if the file is very large, a naive 076 * implementation will effectively put the whole file in memory. On many systems with paging 077 * and virtual memory, this is not a problem - because it is mapped read-only, the kernel 078 * can always page it to disk "for free". However, on systems where killing processes 079 * happens all the time in normal conditions (i.e., android) the OS must make a tradeoff 080 * between paging memory and killing other processes - so allocating a gigantic buffer and 081 * then sequentially accessing it could result in other processes dying. This is solvable 082 * via madvise(2), but that obviously doesn't exist in java. 083 * <li>Ordinary copy. Kernel copies bytes into a kernel buffer, from a kernel buffer into a 084 * userspace buffer (byte[] or ByteBuffer), then copies them from that buffer into the 085 * destination channel. 086 * </ol> 087 * 088 * This value is intended to be large enough to make the overhead of system calls negligible, 089 * without being so large that it causes problems for systems with atypical memory management if 090 * approaches 2 or 3 are used. 091 */ 092 private static final int ZERO_COPY_CHUNK_SIZE = 512 * 1024; 093 094 private ByteStreams() {} 095 096 /** 097 * Copies all bytes from the input stream to the output stream. Does not close or flush either 098 * stream. 099 * 100 * <p><b>Java 9 users and later:</b> this method should be treated as deprecated; use the 101 * equivalent {@link InputStream#transferTo} method instead. 102 * 103 * @param from the input stream to read from 104 * @param to the output stream to write to 105 * @return the number of bytes copied 106 * @throws IOException if an I/O error occurs 107 */ 108 @CanIgnoreReturnValue 109 public static long copy(InputStream from, OutputStream to) throws IOException { 110 checkNotNull(from); 111 checkNotNull(to); 112 byte[] buf = createBuffer(); 113 long total = 0; 114 while (true) { 115 int r = from.read(buf); 116 if (r == -1) { 117 break; 118 } 119 to.write(buf, 0, r); 120 total += r; 121 } 122 return total; 123 } 124 125 /** 126 * Copies all bytes from the readable channel to the writable channel. Does not close or flush 127 * either channel. 128 * 129 * @param from the readable channel to read from 130 * @param to the writable channel to write to 131 * @return the number of bytes copied 132 * @throws IOException if an I/O error occurs 133 */ 134 @J2ktIncompatible 135 @CanIgnoreReturnValue 136 public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException { 137 checkNotNull(from); 138 checkNotNull(to); 139 if (from instanceof FileChannel) { 140 FileChannel sourceChannel = (FileChannel) from; 141 long oldPosition = sourceChannel.position(); 142 long position = oldPosition; 143 long copied; 144 do { 145 copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to); 146 position += copied; 147 sourceChannel.position(position); 148 } while (copied > 0 || position < sourceChannel.size()); 149 return position - oldPosition; 150 } 151 152 ByteBuffer buf = ByteBuffer.wrap(createBuffer()); 153 long total = 0; 154 while (from.read(buf) != -1) { 155 Java8Compatibility.flip(buf); 156 while (buf.hasRemaining()) { 157 total += to.write(buf); 158 } 159 Java8Compatibility.clear(buf); 160 } 161 return total; 162 } 163 164 /** Max array length on JVM. */ 165 private static final int MAX_ARRAY_LEN = Integer.MAX_VALUE - 8; 166 167 /** Large enough to never need to expand, given the geometric progression of buffer sizes. */ 168 private static final int TO_BYTE_ARRAY_DEQUE_SIZE = 20; 169 170 /** 171 * Returns a byte array containing the bytes from the buffers already in {@code bufs} (which have 172 * a total combined length of {@code totalLen} bytes) followed by all bytes remaining in the given 173 * input stream. 174 */ 175 private static byte[] toByteArrayInternal(InputStream in, Queue<byte[]> bufs, int totalLen) 176 throws IOException { 177 // Roughly size to match what has been read already. Some file systems, such as procfs, return 0 178 // as their length. These files are very small, so it's wasteful to allocate an 8KB buffer. 179 int initialBufferSize = min(BUFFER_SIZE, max(128, Integer.highestOneBit(totalLen) * 2)); 180 // Starting with an 8k buffer, double the size of each successive buffer. Smaller buffers 181 // quadruple in size until they reach 8k, to minimize the number of small reads for longer 182 // streams. Buffers are retained in a deque so that there's no copying between buffers while 183 // reading and so all of the bytes in each new allocated buffer are available for reading from 184 // the stream. 185 for (int bufSize = initialBufferSize; 186 totalLen < MAX_ARRAY_LEN; 187 bufSize = IntMath.saturatedMultiply(bufSize, bufSize < 4096 ? 4 : 2)) { 188 byte[] buf = new byte[min(bufSize, MAX_ARRAY_LEN - totalLen)]; 189 bufs.add(buf); 190 int off = 0; 191 while (off < buf.length) { 192 // always OK to fill buf; its size plus the rest of bufs is never more than MAX_ARRAY_LEN 193 int r = in.read(buf, off, buf.length - off); 194 if (r == -1) { 195 return combineBuffers(bufs, totalLen); 196 } 197 off += r; 198 totalLen += r; 199 } 200 } 201 202 // read MAX_ARRAY_LEN bytes without seeing end of stream 203 if (in.read() == -1) { 204 // oh, there's the end of the stream 205 return combineBuffers(bufs, MAX_ARRAY_LEN); 206 } else { 207 throw new OutOfMemoryError("input is too large to fit in a byte array"); 208 } 209 } 210 211 private static byte[] combineBuffers(Queue<byte[]> bufs, int totalLen) { 212 if (bufs.isEmpty()) { 213 return new byte[0]; 214 } 215 byte[] result = bufs.remove(); 216 if (result.length == totalLen) { 217 return result; 218 } 219 int remaining = totalLen - result.length; 220 result = Arrays.copyOf(result, totalLen); 221 while (remaining > 0) { 222 byte[] buf = bufs.remove(); 223 int bytesToCopy = min(remaining, buf.length); 224 int resultOffset = totalLen - remaining; 225 System.arraycopy(buf, 0, result, resultOffset, bytesToCopy); 226 remaining -= bytesToCopy; 227 } 228 return result; 229 } 230 231 /** 232 * Reads all bytes from an input stream into a byte array. Does not close the stream. 233 * 234 * <p><b>Java 9+ users:</b> use {@code in#readAllBytes()} instead. 235 * 236 * @param in the input stream to read from 237 * @return a byte array containing all the bytes from the stream 238 * @throws IOException if an I/O error occurs 239 */ 240 public static byte[] toByteArray(InputStream in) throws IOException { 241 checkNotNull(in); 242 return toByteArrayInternal(in, new ArrayDeque<byte[]>(TO_BYTE_ARRAY_DEQUE_SIZE), 0); 243 } 244 245 /** 246 * Reads all bytes from an input stream into a byte array. The given expected size is used to 247 * create an initial byte array, but if the actual number of bytes read from the stream differs, 248 * the correct result will be returned anyway. 249 */ 250 static byte[] toByteArray(InputStream in, long expectedSize) throws IOException { 251 checkArgument(expectedSize >= 0, "expectedSize (%s) must be non-negative", expectedSize); 252 if (expectedSize > MAX_ARRAY_LEN) { 253 throw new OutOfMemoryError(expectedSize + " bytes is too large to fit in a byte array"); 254 } 255 256 byte[] bytes = new byte[(int) expectedSize]; 257 int remaining = (int) expectedSize; 258 259 while (remaining > 0) { 260 int off = (int) expectedSize - remaining; 261 int read = in.read(bytes, off, remaining); 262 if (read == -1) { 263 // end of stream before reading expectedSize bytes 264 // just return the bytes read so far 265 return Arrays.copyOf(bytes, off); 266 } 267 remaining -= read; 268 } 269 270 // bytes is now full 271 int b = in.read(); 272 if (b == -1) { 273 return bytes; 274 } 275 276 // the stream was longer, so read the rest normally 277 Queue<byte[]> bufs = new ArrayDeque<>(TO_BYTE_ARRAY_DEQUE_SIZE + 2); 278 bufs.add(bytes); 279 bufs.add(new byte[] {(byte) b}); 280 return toByteArrayInternal(in, bufs, bytes.length + 1); 281 } 282 283 /** 284 * Reads and discards data from the given {@code InputStream} until the end of the stream is 285 * reached. Returns the total number of bytes read. Does not close the stream. 286 * 287 * @since 20.0 288 */ 289 @CanIgnoreReturnValue 290 public static long exhaust(InputStream in) throws IOException { 291 long total = 0; 292 long read; 293 byte[] buf = createBuffer(); 294 while ((read = in.read(buf)) != -1) { 295 total += read; 296 } 297 return total; 298 } 299 300 /** 301 * Returns a new {@link ByteArrayDataInput} instance to read from the {@code bytes} array from the 302 * beginning. 303 */ 304 @J2ktIncompatible 305 public static ByteArrayDataInput newDataInput(byte[] bytes) { 306 return newDataInput(new ByteArrayInputStream(bytes)); 307 } 308 309 /** 310 * Returns a new {@link ByteArrayDataInput} instance to read from the {@code bytes} array, 311 * starting at the given position. 312 * 313 * @throws IndexOutOfBoundsException if {@code start} is negative or greater than the length of 314 * the array 315 */ 316 @J2ktIncompatible 317 public static ByteArrayDataInput newDataInput(byte[] bytes, int start) { 318 checkPositionIndex(start, bytes.length); 319 return newDataInput(new ByteArrayInputStream(bytes, start, bytes.length - start)); 320 } 321 322 /** 323 * Returns a new {@link ByteArrayDataInput} instance to read from the given {@code 324 * ByteArrayInputStream}. The given input stream is not reset before being read from by the 325 * returned {@code ByteArrayDataInput}. 326 * 327 * @since 17.0 328 */ 329 @J2ktIncompatible 330 public static ByteArrayDataInput newDataInput(ByteArrayInputStream byteArrayInputStream) { 331 return new ByteArrayDataInputStream(checkNotNull(byteArrayInputStream)); 332 } 333 334 @J2ktIncompatible 335 private static class ByteArrayDataInputStream implements ByteArrayDataInput { 336 final DataInput input; 337 338 ByteArrayDataInputStream(ByteArrayInputStream byteArrayInputStream) { 339 this.input = new DataInputStream(byteArrayInputStream); 340 } 341 342 @Override 343 public void readFully(byte[] b) { 344 try { 345 input.readFully(b); 346 } catch (IOException e) { 347 throw new IllegalStateException(e); 348 } 349 } 350 351 @Override 352 public void readFully(byte[] b, int off, int len) { 353 try { 354 input.readFully(b, off, len); 355 } catch (IOException e) { 356 throw new IllegalStateException(e); 357 } 358 } 359 360 @Override 361 public int skipBytes(int n) { 362 try { 363 return input.skipBytes(n); 364 } catch (IOException e) { 365 throw new IllegalStateException(e); 366 } 367 } 368 369 @Override 370 public boolean readBoolean() { 371 try { 372 return input.readBoolean(); 373 } catch (IOException e) { 374 throw new IllegalStateException(e); 375 } 376 } 377 378 @Override 379 public byte readByte() { 380 try { 381 return input.readByte(); 382 } catch (EOFException e) { 383 throw new IllegalStateException(e); 384 } catch (IOException impossible) { 385 throw new AssertionError(impossible); 386 } 387 } 388 389 @Override 390 public int readUnsignedByte() { 391 try { 392 return input.readUnsignedByte(); 393 } catch (IOException e) { 394 throw new IllegalStateException(e); 395 } 396 } 397 398 @Override 399 public short readShort() { 400 try { 401 return input.readShort(); 402 } catch (IOException e) { 403 throw new IllegalStateException(e); 404 } 405 } 406 407 @Override 408 public int readUnsignedShort() { 409 try { 410 return input.readUnsignedShort(); 411 } catch (IOException e) { 412 throw new IllegalStateException(e); 413 } 414 } 415 416 @Override 417 public char readChar() { 418 try { 419 return input.readChar(); 420 } catch (IOException e) { 421 throw new IllegalStateException(e); 422 } 423 } 424 425 @Override 426 public int readInt() { 427 try { 428 return input.readInt(); 429 } catch (IOException e) { 430 throw new IllegalStateException(e); 431 } 432 } 433 434 @Override 435 public long readLong() { 436 try { 437 return input.readLong(); 438 } catch (IOException e) { 439 throw new IllegalStateException(e); 440 } 441 } 442 443 @Override 444 public float readFloat() { 445 try { 446 return input.readFloat(); 447 } catch (IOException e) { 448 throw new IllegalStateException(e); 449 } 450 } 451 452 @Override 453 public double readDouble() { 454 try { 455 return input.readDouble(); 456 } catch (IOException e) { 457 throw new IllegalStateException(e); 458 } 459 } 460 461 @Override 462 public @Nullable String readLine() { 463 try { 464 return input.readLine(); 465 } catch (IOException e) { 466 throw new IllegalStateException(e); 467 } 468 } 469 470 @Override 471 public String readUTF() { 472 try { 473 return input.readUTF(); 474 } catch (IOException e) { 475 throw new IllegalStateException(e); 476 } 477 } 478 } 479 480 /** Returns a new {@link ByteArrayDataOutput} instance with a default size. */ 481 @J2ktIncompatible 482 public static ByteArrayDataOutput newDataOutput() { 483 return newDataOutput(new ByteArrayOutputStream()); 484 } 485 486 /** 487 * Returns a new {@link ByteArrayDataOutput} instance sized to hold {@code size} bytes before 488 * resizing. 489 * 490 * @throws IllegalArgumentException if {@code size} is negative 491 */ 492 @J2ktIncompatible 493 public static ByteArrayDataOutput newDataOutput(int size) { 494 // When called at high frequency, boxing size generates too much garbage, 495 // so avoid doing that if we can. 496 if (size < 0) { 497 throw new IllegalArgumentException(String.format("Invalid size: %s", size)); 498 } 499 return newDataOutput(new ByteArrayOutputStream(size)); 500 } 501 502 /** 503 * Returns a new {@link ByteArrayDataOutput} instance which writes to the given {@code 504 * ByteArrayOutputStream}. The given output stream is not reset before being written to by the 505 * returned {@code ByteArrayDataOutput} and new data will be appended to any existing content. 506 * 507 * <p>Note that if the given output stream was not empty or is modified after the {@code 508 * ByteArrayDataOutput} is created, the contract for {@link ByteArrayDataOutput#toByteArray} will 509 * not be honored (the bytes returned in the byte array may not be exactly what was written via 510 * calls to {@code ByteArrayDataOutput}). 511 * 512 * @since 17.0 513 */ 514 @J2ktIncompatible 515 public static ByteArrayDataOutput newDataOutput(ByteArrayOutputStream byteArrayOutputStream) { 516 return new ByteArrayDataOutputStream(checkNotNull(byteArrayOutputStream)); 517 } 518 519 @J2ktIncompatible 520 private static class ByteArrayDataOutputStream implements ByteArrayDataOutput { 521 522 final DataOutput output; 523 final ByteArrayOutputStream byteArrayOutputStream; 524 525 ByteArrayDataOutputStream(ByteArrayOutputStream byteArrayOutputStream) { 526 this.byteArrayOutputStream = byteArrayOutputStream; 527 output = new DataOutputStream(byteArrayOutputStream); 528 } 529 530 @Override 531 public void write(int b) { 532 try { 533 output.write(b); 534 } catch (IOException impossible) { 535 throw new AssertionError(impossible); 536 } 537 } 538 539 @Override 540 public void write(byte[] b) { 541 try { 542 output.write(b); 543 } catch (IOException impossible) { 544 throw new AssertionError(impossible); 545 } 546 } 547 548 @Override 549 public void write(byte[] b, int off, int len) { 550 try { 551 output.write(b, off, len); 552 } catch (IOException impossible) { 553 throw new AssertionError(impossible); 554 } 555 } 556 557 @Override 558 public void writeBoolean(boolean v) { 559 try { 560 output.writeBoolean(v); 561 } catch (IOException impossible) { 562 throw new AssertionError(impossible); 563 } 564 } 565 566 @Override 567 public void writeByte(int v) { 568 try { 569 output.writeByte(v); 570 } catch (IOException impossible) { 571 throw new AssertionError(impossible); 572 } 573 } 574 575 @Override 576 public void writeBytes(String s) { 577 try { 578 output.writeBytes(s); 579 } catch (IOException impossible) { 580 throw new AssertionError(impossible); 581 } 582 } 583 584 @Override 585 public void writeChar(int v) { 586 try { 587 output.writeChar(v); 588 } catch (IOException impossible) { 589 throw new AssertionError(impossible); 590 } 591 } 592 593 @Override 594 public void writeChars(String s) { 595 try { 596 output.writeChars(s); 597 } catch (IOException impossible) { 598 throw new AssertionError(impossible); 599 } 600 } 601 602 @Override 603 public void writeDouble(double v) { 604 try { 605 output.writeDouble(v); 606 } catch (IOException impossible) { 607 throw new AssertionError(impossible); 608 } 609 } 610 611 @Override 612 public void writeFloat(float v) { 613 try { 614 output.writeFloat(v); 615 } catch (IOException impossible) { 616 throw new AssertionError(impossible); 617 } 618 } 619 620 @Override 621 public void writeInt(int v) { 622 try { 623 output.writeInt(v); 624 } catch (IOException impossible) { 625 throw new AssertionError(impossible); 626 } 627 } 628 629 @Override 630 public void writeLong(long v) { 631 try { 632 output.writeLong(v); 633 } catch (IOException impossible) { 634 throw new AssertionError(impossible); 635 } 636 } 637 638 @Override 639 public void writeShort(int v) { 640 try { 641 output.writeShort(v); 642 } catch (IOException impossible) { 643 throw new AssertionError(impossible); 644 } 645 } 646 647 @Override 648 public void writeUTF(String s) { 649 try { 650 output.writeUTF(s); 651 } catch (IOException impossible) { 652 throw new AssertionError(impossible); 653 } 654 } 655 656 @Override 657 public byte[] toByteArray() { 658 return byteArrayOutputStream.toByteArray(); 659 } 660 } 661 662 private static final OutputStream NULL_OUTPUT_STREAM = 663 new OutputStream() { 664 /** Discards the specified byte. */ 665 @Override 666 public void write(int b) {} 667 668 /** Discards the specified byte array. */ 669 @Override 670 public void write(byte[] b) { 671 checkNotNull(b); 672 } 673 674 /** Discards the specified byte array. */ 675 @Override 676 public void write(byte[] b, int off, int len) { 677 checkNotNull(b); 678 checkPositionIndexes(off, off + len, b.length); 679 } 680 681 @Override 682 public String toString() { 683 return "ByteStreams.nullOutputStream()"; 684 } 685 }; 686 687 /** 688 * Returns an {@link OutputStream} that simply discards written bytes. 689 * 690 * <p><b>Java 11+ users:</b> use {@link OutputStream#nullOutputStream()} instead. Note that the 691 * {@link ByteStreams} method returns a singleton stream whose {@code close} method has no effect, 692 * while the {@link OutputStream} method returns a new instance whose {@code write} methods throw 693 * if called on a closed stream. 694 * 695 * @since 14.0 (since 1.0 as com.google.common.io.NullOutputStream) 696 */ 697 public static OutputStream nullOutputStream() { 698 return NULL_OUTPUT_STREAM; 699 } 700 701 /** 702 * Wraps a {@link InputStream}, limiting the number of bytes which can be read. 703 * 704 * @param in the input stream to be wrapped 705 * @param limit the maximum number of bytes to be read 706 * @return a length-limited {@link InputStream} 707 * @since 14.0 (since 1.0 as com.google.common.io.LimitInputStream) 708 */ 709 @J2ktIncompatible 710 public static InputStream limit(InputStream in, long limit) { 711 return new LimitedInputStream(in, limit); 712 } 713 714 @J2ktIncompatible 715 private static final class LimitedInputStream extends FilterInputStream { 716 717 private long left; 718 private long mark = -1; 719 720 LimitedInputStream(InputStream in, long limit) { 721 super(in); 722 checkNotNull(in); 723 checkArgument(limit >= 0, "limit must be non-negative"); 724 left = limit; 725 } 726 727 @Override 728 public int available() throws IOException { 729 return (int) min(in.available(), left); 730 } 731 732 // it's okay to mark even if mark isn't supported, as reset won't work 733 @Override 734 public synchronized void mark(int readLimit) { 735 in.mark(readLimit); 736 mark = left; 737 } 738 739 @Override 740 public int read() throws IOException { 741 if (left == 0) { 742 return -1; 743 } 744 745 int result = in.read(); 746 if (result != -1) { 747 --left; 748 } 749 return result; 750 } 751 752 @Override 753 public int read(byte[] b, int off, int len) throws IOException { 754 if (left == 0) { 755 return -1; 756 } 757 758 len = (int) min(len, left); 759 int result = in.read(b, off, len); 760 if (result != -1) { 761 left -= result; 762 } 763 return result; 764 } 765 766 @Override 767 public synchronized void reset() throws IOException { 768 if (!in.markSupported()) { 769 throw new IOException("Mark not supported"); 770 } 771 if (mark == -1) { 772 throw new IOException("Mark not set"); 773 } 774 775 in.reset(); 776 left = mark; 777 } 778 779 @Override 780 public long skip(long n) throws IOException { 781 n = min(n, left); 782 long skipped = in.skip(n); 783 left -= skipped; 784 return skipped; 785 } 786 } 787 788 /** 789 * Attempts to read enough bytes from the stream to fill the given byte array, with the same 790 * behavior as {@link DataInput#readFully(byte[])}. Does not close the stream. 791 * 792 * @param in the input stream to read from. 793 * @param b the buffer into which the data is read. 794 * @throws EOFException if this stream reaches the end before reading all the bytes. 795 * @throws IOException if an I/O error occurs. 796 */ 797 public static void readFully(InputStream in, byte[] b) throws IOException { 798 readFully(in, b, 0, b.length); 799 } 800 801 /** 802 * Attempts to read {@code len} bytes from the stream into the given array starting at {@code 803 * off}, with the same behavior as {@link DataInput#readFully(byte[], int, int)}. Does not close 804 * the stream. 805 * 806 * @param in the input stream to read from. 807 * @param b the buffer into which the data is read. 808 * @param off an int specifying the offset into the data. 809 * @param len an int specifying the number of bytes to read. 810 * @throws EOFException if this stream reaches the end before reading all the bytes. 811 * @throws IOException if an I/O error occurs. 812 */ 813 public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException { 814 int read = read(in, b, off, len); 815 if (read != len) { 816 throw new EOFException( 817 "reached end of stream after reading " + read + " bytes; " + len + " bytes expected"); 818 } 819 } 820 821 /** 822 * Discards {@code n} bytes of data from the input stream. This method will block until the full 823 * amount has been skipped. Does not close the stream. 824 * 825 * @param in the input stream to read from 826 * @param n the number of bytes to skip 827 * @throws EOFException if this stream reaches the end before skipping all the bytes 828 * @throws IOException if an I/O error occurs, or the stream does not support skipping 829 */ 830 public static void skipFully(InputStream in, long n) throws IOException { 831 long skipped = skipUpTo(in, n); 832 if (skipped < n) { 833 throw new EOFException( 834 "reached end of stream after skipping " + skipped + " bytes; " + n + " bytes expected"); 835 } 836 } 837 838 /** 839 * Discards up to {@code n} bytes of data from the input stream. This method will block until 840 * either the full amount has been skipped or until the end of the stream is reached, whichever 841 * happens first. Returns the total number of bytes skipped. 842 */ 843 static long skipUpTo(InputStream in, long n) throws IOException { 844 long totalSkipped = 0; 845 // A buffer is allocated if skipSafely does not skip any bytes. 846 byte[] buf = null; 847 848 while (totalSkipped < n) { 849 long remaining = n - totalSkipped; 850 long skipped = skipSafely(in, remaining); 851 852 if (skipped == 0) { 853 // Do a buffered read since skipSafely could return 0 repeatedly, for example if 854 // in.available() always returns 0 (the default). 855 int skip = (int) min(remaining, BUFFER_SIZE); 856 if (buf == null) { 857 // Allocate a buffer bounded by the maximum size that can be requested, for 858 // example an array of BUFFER_SIZE is unnecessary when the value of remaining 859 // is smaller. 860 buf = new byte[skip]; 861 } 862 if ((skipped = in.read(buf, 0, skip)) == -1) { 863 // Reached EOF 864 break; 865 } 866 } 867 868 totalSkipped += skipped; 869 } 870 871 return totalSkipped; 872 } 873 874 /** 875 * Attempts to skip up to {@code n} bytes from the given input stream, but not more than {@code 876 * in.available()} bytes. This prevents {@code FileInputStream} from skipping more bytes than 877 * actually remain in the file, something that it {@linkplain java.io.FileInputStream#skip(long) 878 * specifies} it can do in its Javadoc despite the fact that it is violating the contract of 879 * {@code InputStream.skip()}. 880 */ 881 private static long skipSafely(InputStream in, long n) throws IOException { 882 int available = in.available(); 883 return available == 0 ? 0 : in.skip(min(available, n)); 884 } 885 886 /** 887 * Process the bytes of the given input stream using the given processor. 888 * 889 * @param input the input stream to process 890 * @param processor the object to which to pass the bytes of the stream 891 * @return the result of the byte processor 892 * @throws IOException if an I/O error occurs 893 * @since 14.0 894 */ 895 @CanIgnoreReturnValue // some processors won't return a useful result 896 @ParametricNullness 897 @J2ktIncompatible 898 public static <T extends @Nullable Object> T readBytes( 899 InputStream input, ByteProcessor<T> processor) throws IOException { 900 checkNotNull(input); 901 checkNotNull(processor); 902 903 byte[] buf = createBuffer(); 904 int read; 905 do { 906 read = input.read(buf); 907 } while (read != -1 && processor.processBytes(buf, 0, read)); 908 return processor.getResult(); 909 } 910 911 /** 912 * Reads some bytes from an input stream and stores them into the buffer array {@code b}. This 913 * method blocks until {@code len} bytes of input data have been read into the array, or end of 914 * file is detected. The number of bytes read is returned, possibly zero. Does not close the 915 * stream. 916 * 917 * <p>A caller can detect EOF if the number of bytes read is less than {@code len}. All subsequent 918 * calls on the same stream will return zero. 919 * 920 * <p>If {@code b} is null, a {@code NullPointerException} is thrown. If {@code off} is negative, 921 * or {@code len} is negative, or {@code off+len} is greater than the length of the array {@code 922 * b}, then an {@code IndexOutOfBoundsException} is thrown. If {@code len} is zero, then no bytes 923 * are read. Otherwise, the first byte read is stored into element {@code b[off]}, the next one 924 * into {@code b[off+1]}, and so on. The number of bytes read is, at most, equal to {@code len}. 925 * 926 * @param in the input stream to read from 927 * @param b the buffer into which the data is read 928 * @param off an int specifying the offset into the data 929 * @param len an int specifying the number of bytes to read 930 * @return the number of bytes read 931 * @throws IOException if an I/O error occurs 932 * @throws IndexOutOfBoundsException if {@code off} is negative, if {@code len} is negative, or if 933 * {@code off + len} is greater than {@code b.length} 934 */ 935 @CanIgnoreReturnValue 936 // Sometimes you don't care how many bytes you actually read, I guess. 937 // (You know that it's either going to read len bytes or stop at EOF.) 938 public static int read(InputStream in, byte[] b, int off, int len) throws IOException { 939 checkNotNull(in); 940 checkNotNull(b); 941 if (len < 0) { 942 throw new IndexOutOfBoundsException(String.format("len (%s) cannot be negative", len)); 943 } 944 checkPositionIndexes(off, off + len, b.length); 945 int total = 0; 946 while (total < len) { 947 int result = in.read(b, off + total, len - total); 948 if (result == -1) { 949 break; 950 } 951 total += result; 952 } 953 return total; 954 } 955}