001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.io;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021
022import com.google.common.collect.ImmutableList;
023import com.google.common.hash.Funnels;
024import com.google.common.hash.HashCode;
025import com.google.common.hash.HashFunction;
026import com.google.common.hash.Hasher;
027
028import java.io.BufferedInputStream;
029import java.io.ByteArrayInputStream;
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.InputStreamReader;
033import java.io.OutputStream;
034import java.io.Reader;
035import java.nio.charset.Charset;
036import java.util.Arrays;
037import java.util.Iterator;
038
039/**
040 * A readable source of bytes, such as a file. Unlike an {@link InputStream}, a
041 * {@code ByteSource} is not an open, stateful stream for input that can be read and closed.
042 * Instead, it is an immutable <i>supplier</i> of {@code InputStream} instances.
043 *
044 * <p>{@code ByteSource} provides two kinds of methods:
045 * <ul>
046 *   <li><b>Methods that return a stream:</b> These methods should return a <i>new</i>, independent
047 *   instance each time they are called. The caller is responsible for ensuring that the returned
048 *   stream is closed.
049 *   <li><b>Convenience methods:</b> These are implementations of common operations that are
050 *   typically implemented by opening a stream using one of the methods in the first category, doing
051 *   something and finally closing the stream that was opened.
052 * </ul>
053 *
054 * @since 14.0
055 * @author Colin Decker
056 */
057public abstract class ByteSource implements InputSupplier<InputStream> {
058
059  private static final int BUF_SIZE = 0x1000; // 4K
060
061  /**
062   * Returns a {@link CharSource} view of this byte source that decodes bytes read from this source
063   * as characters using the given {@link Charset}.
064   */
065  public CharSource asCharSource(Charset charset) {
066    return new AsCharSource(charset);
067  }
068
069  /**
070   * Opens a new {@link InputStream} for reading from this source. This method should return a new,
071   * independent stream each time it is called.
072   *
073   * <p>The caller is responsible for ensuring that the returned stream is closed.
074   *
075   * @throws IOException if an I/O error occurs in the process of opening the stream
076   */
077  public abstract InputStream openStream() throws IOException;
078
079  /**
080   * This method is a temporary method provided for easing migration from suppliers to sources and
081   * sinks.
082   *
083   * @since 15.0
084   * @deprecated This method is only provided for temporary compatibility with the
085   *     {@link InputSupplier} interface and should not be called directly. Use {@link #openStream}
086   *     instead.
087   */
088  @Override
089  @Deprecated
090  public final InputStream getInput() throws IOException {
091    return openStream();
092  }
093
094  /**
095   * Opens a new buffered {@link InputStream} for reading from this source. The returned stream is
096   * not required to be a {@link BufferedInputStream} in order to allow implementations to simply
097   * delegate to {@link #openStream()} when the stream returned by that method does not benefit
098   * from additional buffering (for example, a {@code ByteArrayInputStream}). This method should
099   * return a new, independent stream each time it is called.
100   *
101   * <p>The caller is responsible for ensuring that the returned stream is closed.
102   *
103   * @throws IOException if an I/O error occurs in the process of opening the stream
104   * @since 15.0 (in 14.0 with return type {@link BufferedInputStream})
105   */
106  public InputStream openBufferedStream() throws IOException {
107    InputStream in = openStream();
108    return (in instanceof BufferedInputStream)
109        ? (BufferedInputStream) in
110        : new BufferedInputStream(in);
111  }
112
113  /**
114   * Returns a view of a slice of this byte source that is at most {@code length} bytes long
115   * starting at the given {@code offset}.
116   *
117   * @throws IllegalArgumentException if {@code offset} or {@code length} is negative
118   */
119  public ByteSource slice(long offset, long length) {
120    return new SlicedByteSource(offset, length);
121  }
122
123  /**
124   * Returns whether the source has zero bytes. The default implementation is to open a stream and
125   * check for EOF.
126   *
127   * @throws IOException if an I/O error occurs
128   * @since 15.0
129   */
130  public boolean isEmpty() throws IOException {
131    Closer closer = Closer.create();
132    try {
133      InputStream in = closer.register(openStream());
134      return in.read() == -1;
135    } catch (Throwable e) {
136      throw closer.rethrow(e);
137    } finally {
138      closer.close();
139    }
140  }
141
142  /**
143   * Returns the size of this source in bytes. For most implementations, this is a heavyweight
144   * operation that will open a stream, read (or {@link InputStream#skip(long) skip}, if possible)
145   * to the end of the stream and return the total number of bytes that were read.
146   *
147   * <p>For some sources, such as a file, this method may use a more efficient implementation. Note
148   * that in such cases, it is <i>possible</i> that this method will return a different number of
149   * bytes than would be returned by reading all of the bytes (for example, some special files may
150   * return a size of 0 despite actually having content when read).
151   *
152   * <p>In either case, if this is a mutable source such as a file, the size it returns may not be
153   * the same number of bytes a subsequent read would return.
154   *
155   * @throws IOException if an I/O error occurs in the process of reading the size of this source
156   */
157  public long size() throws IOException {
158    Closer closer = Closer.create();
159    try {
160      InputStream in = closer.register(openStream());
161      return countBySkipping(in);
162    } catch (IOException e) {
163      // skip may not be supported... at any rate, try reading
164    } finally {
165      closer.close();
166    }
167
168    closer = Closer.create();
169    try {
170      InputStream in = closer.register(openStream());
171      return countByReading(in);
172    } catch (Throwable e) {
173      throw closer.rethrow(e);
174    } finally {
175      closer.close();
176    }
177  }
178
179  /**
180   * Counts the bytes in the given input stream using skip if possible. Returns SKIP_FAILED if the
181   * first call to skip threw, in which case skip may just not be supported.
182   */
183  private long countBySkipping(InputStream in) throws IOException {
184    long count = 0;
185    while (true) {
186      // don't try to skip more than available()
187      // things may work really wrong with FileInputStream otherwise
188      long skipped = in.skip(Math.min(in.available(), Integer.MAX_VALUE));
189      if (skipped <= 0) {
190        if (in.read() == -1) {
191          return count;
192        } else if (count == 0 && in.available() == 0) {
193          // if available is still zero after reading a single byte, it
194          // will probably always be zero, so we should countByReading
195          throw new IOException();
196        }
197        count++;
198      } else {
199        count += skipped;
200      }
201    }
202  }
203
204  private static final byte[] countBuffer = new byte[BUF_SIZE];
205
206  private long countByReading(InputStream in) throws IOException {
207    long count = 0;
208    long read;
209    while ((read = in.read(countBuffer)) != -1) {
210      count += read;
211    }
212    return count;
213  }
214
215  /**
216   * Copies the contents of this byte source to the given {@code OutputStream}. Does not close
217   * {@code output}.
218   *
219   * @throws IOException if an I/O error occurs in the process of reading from this source or
220   *     writing to {@code output}
221   */
222  public long copyTo(OutputStream output) throws IOException {
223    checkNotNull(output);
224
225    Closer closer = Closer.create();
226    try {
227      InputStream in = closer.register(openStream());
228      return ByteStreams.copy(in, output);
229    } catch (Throwable e) {
230      throw closer.rethrow(e);
231    } finally {
232      closer.close();
233    }
234  }
235
236  /**
237   * Copies the contents of this byte source to the given {@code ByteSink}.
238   *
239   * @throws IOException if an I/O error occurs in the process of reading from this source or
240   *     writing to {@code sink}
241   */
242  public long copyTo(ByteSink sink) throws IOException {
243    checkNotNull(sink);
244
245    Closer closer = Closer.create();
246    try {
247      InputStream in = closer.register(openStream());
248      OutputStream out = closer.register(sink.openStream());
249      return ByteStreams.copy(in, out);
250    } catch (Throwable e) {
251      throw closer.rethrow(e);
252    } finally {
253      closer.close();
254    }
255  }
256
257  /**
258   * Reads the full contents of this byte source as a byte array.
259   *
260   * @throws IOException if an I/O error occurs in the process of reading from this source
261   */
262  public byte[] read() throws IOException {
263    Closer closer = Closer.create();
264    try {
265      InputStream in = closer.register(openStream());
266      return ByteStreams.toByteArray(in);
267    } catch (Throwable e) {
268      throw closer.rethrow(e);
269    } finally {
270      closer.close();
271    }
272  }
273
274  /**
275   * Hashes the contents of this byte source using the given hash function.
276   *
277   * @throws IOException if an I/O error occurs in the process of reading from this source
278   */
279  public HashCode hash(HashFunction hashFunction) throws IOException {
280    Hasher hasher = hashFunction.newHasher();
281    copyTo(Funnels.asOutputStream(hasher));
282    return hasher.hash();
283  }
284
285  /**
286   * Checks that the contents of this byte source are equal to the contents of the given byte
287   * source.
288   *
289   * @throws IOException if an I/O error occurs in the process of reading from this source or
290   *     {@code other}
291   */
292  public boolean contentEquals(ByteSource other) throws IOException {
293    checkNotNull(other);
294
295    byte[] buf1 = new byte[BUF_SIZE];
296    byte[] buf2 = new byte[BUF_SIZE];
297
298    Closer closer = Closer.create();
299    try {
300      InputStream in1 = closer.register(openStream());
301      InputStream in2 = closer.register(other.openStream());
302      while (true) {
303        int read1 = ByteStreams.read(in1, buf1, 0, BUF_SIZE);
304        int read2 = ByteStreams.read(in2, buf2, 0, BUF_SIZE);
305        if (read1 != read2 || !Arrays.equals(buf1, buf2)) {
306          return false;
307        } else if (read1 != BUF_SIZE) {
308          return true;
309        }
310      }
311    } catch (Throwable e) {
312      throw closer.rethrow(e);
313    } finally {
314      closer.close();
315    }
316  }
317
318  /**
319   * Concatenates multiple {@link ByteSource} instances into a single source.
320   * Streams returned from the source will contain the concatenated data from
321   * the streams of the underlying sources.
322   *
323   * <p>Only one underlying stream will be open at a time. Closing the
324   * concatenated stream will close the open underlying stream.
325   *
326   * @param sources the sources to concatenate
327   * @return a {@code ByteSource} containing the concatenated data
328   * @throws NullPointerException if any of {@code sources} is {@code null}
329   * @since 15.0
330   */
331  public static ByteSource concat(Iterable<? extends ByteSource> sources) {
332    return new ConcatenatedByteSource(sources);
333  }
334
335  /**
336   * Concatenates multiple {@link ByteSource} instances into a single source.
337   * Streams returned from the source will contain the concatenated data from
338   * the streams of the underlying sources.
339   *
340   * <p>Only one underlying stream will be open at a time. Closing the
341   * concatenated stream will close the open underlying stream.
342   *
343   * @param sources the sources to concatenate
344   * @return a {@code ByteSource} containing the concatenated data
345   * @throws NullPointerException if any of {@code sources} is {@code null}
346   * @since 15.0
347   */
348  public static ByteSource concat(Iterator<? extends ByteSource> sources) {
349    return concat(ImmutableList.copyOf(sources));
350  }
351
352  /**
353   * Concatenates multiple {@link ByteSource} instances into a single source.
354   * Streams returned from the source will contain the concatenated data from
355   * the streams of the underlying sources.
356   *
357   * <p>Only one underlying stream will be open at a time. Closing the
358   * concatenated stream will close the open underlying stream.
359   *
360   * @param sources the sources to concatenate
361   * @return a {@code ByteSource} containing the concatenated data
362   * @throws NullPointerException if any of {@code sources} is {@code null}
363   * @since 15.0
364   */
365  public static ByteSource concat(ByteSource... sources) {
366    return concat(ImmutableList.copyOf(sources));
367  }
368
369  /**
370   * Returns a view of the given byte array as a {@link ByteSource}. To view only a specific range
371   * in the array, use {@code ByteSource.wrap(b).slice(offset, length)}.
372   *
373   * @since 15.0 (since 14.0 as {@code ByteStreams.asByteSource(byte[])}).
374   */
375  public static ByteSource wrap(byte[] b) {
376    return new ByteArrayByteSource(b);
377  }
378
379  /**
380   * Returns an immutable {@link ByteSource} that contains no bytes.
381   *
382   * @since 15.0
383   */
384  public static ByteSource empty() {
385    return EmptyByteSource.INSTANCE;
386  }
387
388  /**
389   * A char source that reads bytes from this source and decodes them as characters using a
390   * charset.
391   */
392  private final class AsCharSource extends CharSource {
393
394    private final Charset charset;
395
396    private AsCharSource(Charset charset) {
397      this.charset = checkNotNull(charset);
398    }
399
400    @Override
401    public Reader openStream() throws IOException {
402      return new InputStreamReader(ByteSource.this.openStream(), charset);
403    }
404
405    @Override
406    public String toString() {
407      return ByteSource.this.toString() + ".asCharSource(" + charset + ")";
408    }
409  }
410
411  /**
412   * A view of a subsection of the containing byte source.
413   */
414  private final class SlicedByteSource extends ByteSource {
415
416    private final long offset;
417    private final long length;
418
419    private SlicedByteSource(long offset, long length) {
420      checkArgument(offset >= 0, "offset (%s) may not be negative", offset);
421      checkArgument(length >= 0, "length (%s) may not be negative", length);
422      this.offset = offset;
423      this.length = length;
424    }
425
426    @Override
427    public InputStream openStream() throws IOException {
428      return sliceStream(ByteSource.this.openStream());
429    }
430
431    @Override
432    public InputStream openBufferedStream() throws IOException {
433      return sliceStream(ByteSource.this.openBufferedStream());
434    }
435
436    private InputStream sliceStream(InputStream in) throws IOException {
437      if (offset > 0) {
438        try {
439          ByteStreams.skipFully(in, offset);
440        } catch (Throwable e) {
441          Closer closer = Closer.create();
442          closer.register(in);
443          try {
444            throw closer.rethrow(e);
445          } finally {
446            closer.close();
447          }
448        }
449      }
450      return ByteStreams.limit(in, length);
451    }
452
453    @Override
454    public ByteSource slice(long offset, long length) {
455      checkArgument(offset >= 0, "offset (%s) may not be negative", offset);
456      checkArgument(length >= 0, "length (%s) may not be negative", length);
457      long maxLength = this.length - offset;
458      return ByteSource.this.slice(this.offset + offset, Math.min(length, maxLength));
459    }
460
461    @Override
462    public boolean isEmpty() throws IOException {
463      return length == 0 || super.isEmpty();
464    }
465
466    @Override
467    public String toString() {
468      return ByteSource.this.toString() + ".slice(" + offset + ", " + length + ")";
469    }
470  }
471
472  private static class ByteArrayByteSource extends ByteSource {
473
474    protected final byte[] bytes;
475
476    protected ByteArrayByteSource(byte[] bytes) {
477      this.bytes = checkNotNull(bytes);
478    }
479
480    @Override
481    public InputStream openStream() {
482      return new ByteArrayInputStream(bytes);
483    }
484
485    @Override
486    public InputStream openBufferedStream() throws IOException {
487      return openStream();
488    }
489
490    @Override
491    public boolean isEmpty() {
492      return bytes.length == 0;
493    }
494
495    @Override
496    public long size() {
497      return bytes.length;
498    }
499
500    @Override
501    public byte[] read() {
502      return bytes.clone();
503    }
504
505    @Override
506    public long copyTo(OutputStream output) throws IOException {
507      output.write(bytes);
508      return bytes.length;
509    }
510
511    @Override
512    public HashCode hash(HashFunction hashFunction) throws IOException {
513      return hashFunction.hashBytes(bytes);
514    }
515
516    // TODO(user): Possibly override slice()
517
518    @Override
519    public String toString() {
520      return "ByteSource.wrap(" + BaseEncoding.base16().encode(bytes) + ")";
521    }
522  }
523
524  private static final class EmptyByteSource extends ByteArrayByteSource {
525
526    private static final EmptyByteSource INSTANCE = new EmptyByteSource();
527
528    private EmptyByteSource() {
529      super(new byte[0]);
530    }
531
532    @Override
533    public CharSource asCharSource(Charset charset) {
534      checkNotNull(charset);
535      return CharSource.empty();
536    }
537
538    @Override
539    public byte[] read() {
540      return bytes; // length is 0, no need to clone
541    }
542
543    @Override
544    public String toString() {
545      return "ByteSource.empty()";
546    }
547  }
548
549  private static final class ConcatenatedByteSource extends ByteSource {
550
551    private final ImmutableList<ByteSource> sources;
552
553    ConcatenatedByteSource(Iterable<? extends ByteSource> sources) {
554      this.sources = ImmutableList.copyOf(sources);
555    }
556
557    @Override
558    public InputStream openStream() throws IOException {
559      return new MultiInputStream(sources.iterator());
560    }
561
562    @Override
563    public boolean isEmpty() throws IOException {
564      for (ByteSource source : sources) {
565        if (!source.isEmpty()) {
566          return false;
567        }
568      }
569      return true;
570    }
571
572    @Override
573    public long size() throws IOException {
574      long result = 0L;
575      for (ByteSource source : sources) {
576        result += source.size();
577      }
578      return result;
579    }
580
581    @Override
582    public String toString() {
583      return "ByteSource.concat(" + sources + ")";
584    }
585  }
586}