Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.io;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.io.Reader;
A readable source of bytes, such as a file. Unlike an java.io.InputStream, a ByteSource is not an open, stateful stream for input that can be read and closed. Instead, it is an immutable supplier of InputStream instances.

ByteSource provides two kinds of methods:

  • Methods that return a stream: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned stream is closed.
  • Convenience methods: These are implementations of common operations that are typically implemented by opening a stream using one of the methods in the first category, doing something and finally closing the stream that was opened.

Author(s):
Colin Decker
Since:
14.0
 
 public abstract class ByteSource {
 
   private static final int BUF_SIZE = 0x1000; // 4K
 
  
Constructor for use by subclasses.
 
   protected ByteSource() {}

  
Returns a CharSource view of this byte source that decodes bytes read from this source as characters using the given java.nio.charset.Charset.
 
   public CharSource asCharSource(Charset charset) {
     return new AsCharSource(charset);
   }

  
Opens a new java.io.InputStream for reading from this source. This method should return a new, independent stream each time it is called.

The caller is responsible for ensuring that the returned stream is closed.

Throws:
java.io.IOException if an I/O error occurs in the process of opening the stream
 
   public abstract InputStream openStream() throws IOException;

  
Opens a new buffered java.io.InputStream for reading from this source. The returned stream is not required to be a java.io.BufferedInputStream in order to allow implementations to simply delegate to openStream() when the stream returned by that method does not benefit from additional buffering (for example, a ByteArrayInputStream). This method should return a new, independent stream each time it is called.

The caller is responsible for ensuring that the returned stream is closed.

Throws:
java.io.IOException if an I/O error occurs in the process of opening the stream
Since:
15.0 (in 14.0 with return type java.io.BufferedInputStream)
 
   public InputStream openBufferedStream() throws IOException {
     InputStream in = openStream();
    return (in instanceof BufferedInputStream)
        ? (BufferedInputStreamin
        : new BufferedInputStream(in);
  }

  
Returns a view of a slice of this byte source that is at most length bytes long starting at the given offset.

Throws:
java.lang.IllegalArgumentException if offset or length is negative
  public ByteSource slice(long offsetlong length) {
    return new SlicedByteSource(offsetlength);
  }

  
Returns whether the source has zero bytes. The default implementation is to open a stream and check for EOF.

Throws:
java.io.IOException if an I/O error occurs
Since:
15.0
  public boolean isEmpty() throws IOException {
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return in.read() == -1;
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Returns the size of this source in bytes. For most implementations, this is a heavyweight operation that will open a stream, read (or skip, if possible) to the end of the stream and return the total number of bytes that were read.

For some sources, such as a file, this method may use a more efficient implementation. Note that in such cases, it is possible that this method will return a different number of bytes than would be returned by reading all of the bytes (for example, some special files may return a size of 0 despite actually having content when read).

In either case, if this is a mutable source such as a file, the size it returns may not be the same number of bytes a subsequent read would return.

Throws:
java.io.IOException if an I/O error occurs in the process of reading the size of this source
  public long size() throws IOException {
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return countBySkipping(in);
    } catch (IOException e) {
      // skip may not be supported... at any rate, try reading
    } finally {
      closer.close();
    }
    closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return countByReading(in);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Counts the bytes in the given input stream using skip if possible. Returns SKIP_FAILED if the first call to skip threw, in which case skip may just not be supported.
  private long countBySkipping(InputStream inthrows IOException {
    long count = 0;
    while (true) {
      // don't try to skip more than available()
      // things may work really wrong with FileInputStream otherwise
      long skipped = in.skip(Math.min(in.available(), .));
      if (skipped <= 0) {
        if (in.read() == -1) {
          return count;
        } else if (count == 0 && in.available() == 0) {
          // if available is still zero after reading a single byte, it
          // will probably always be zero, so we should countByReading
          throw new IOException();
        }
        count++;
      } else {
        count += skipped;
      }
    }
  }
  private static final byte[] countBuffer = new byte[];
  private long countByReading(InputStream inthrows IOException {
    long count = 0;
    long read;
    while ((read = in.read()) != -1) {
      count += read;
    }
    return count;
  }

  
Copies the contents of this byte source to the given OutputStream. Does not close output.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source or writing to output
  public long copyTo(OutputStream outputthrows IOException {
    checkNotNull(output);
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return ByteStreams.copy(inoutput);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Copies the contents of this byte source to the given ByteSink.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source or writing to sink
  public long copyTo(ByteSink sinkthrows IOException {
    checkNotNull(sink);
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      OutputStream out = closer.register(sink.openStream());
      return ByteStreams.copy(inout);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Reads the full contents of this byte source as a byte array.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source
  public byte[] read() throws IOException {
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return ByteStreams.toByteArray(in);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Reads the contents of this byte source using the given processor to process bytes as they are read. Stops when all bytes have been read or the consumer returns false. Returns the result produced by the processor.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source or if processor throws an IOException
Since:
16.0
  @Beta
  public <T> T read(ByteProcessor<T> processorthrows IOException {
    checkNotNull(processor);
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return ByteStreams.readBytes(inprocessor);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Hashes the contents of this byte source using the given hash function.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source
  public HashCode hash(HashFunction hashFunctionthrows IOException {
    Hasher hasher = hashFunction.newHasher();
    copyTo(Funnels.asOutputStream(hasher));
    return hasher.hash();
  }

  
Checks that the contents of this byte source are equal to the contents of the given byte source.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from this source or other
  public boolean contentEquals(ByteSource otherthrows IOException {
    checkNotNull(other);
    byte[] buf1 = new byte[];
    byte[] buf2 = new byte[];
    Closer closer = Closer.create();
    try {
      InputStream in1 = closer.register(openStream());
      InputStream in2 = closer.register(other.openStream());
      while (true) {
        int read1 = ByteStreams.read(in1buf1, 0, );
        int read2 = ByteStreams.read(in2buf2, 0, );
        if (read1 != read2 || !Arrays.equals(buf1buf2)) {
          return false;
        } else if (read1 != ) {
          return true;
        }
      }
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Concatenates multiple ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.

Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.

Parameters:
sources the sources to concatenate
Returns:
a ByteSource containing the concatenated data
Since:
15.0
  public static ByteSource concat(Iterable<? extends ByteSourcesources) {
    return new ConcatenatedByteSource(sources);
  }

  
Concatenates multiple ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.

Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.

Note: The input Iterator will be copied to an ImmutableList when this method is called. This will fail if the iterator is infinite and may cause problems if the iterator eagerly fetches data for each source when iterated (rather than producing sources that only load data through their streams). Prefer using the concat(java.lang.Iterable) overload if possible.

Parameters:
sources the sources to concatenate
Returns:
a ByteSource containing the concatenated data
Throws:
java.lang.NullPointerException if any of sources is null
Since:
15.0
  public static ByteSource concat(Iterator<? extends ByteSourcesources) {
    return concat(ImmutableList.copyOf(sources));
  }

  
Concatenates multiple ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.

Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.

Parameters:
sources the sources to concatenate
Returns:
a ByteSource containing the concatenated data
Throws:
java.lang.NullPointerException if any of sources is null
Since:
15.0
  public static ByteSource concat(ByteSource... sources) {
    return concat(ImmutableList.copyOf(sources));
  }

  
Returns a view of the given byte array as a ByteSource. To view only a specific range in the array, use ByteSource.wrap(b).slice(offset, length).

Since:
15.0 (since 14.0 as ByteStreams.asByteSource(byte[])).
  public static ByteSource wrap(byte[] b) {
    return new ByteArrayByteSource(b);
  }

  
Returns an immutable ByteSource that contains no bytes.

Since:
15.0
  public static ByteSource empty() {
    return .;
  }

  
A char source that reads bytes from this source and decodes them as characters using a charset.
  private final class AsCharSource extends CharSource {
    private final Charset charset;
    private AsCharSource(Charset charset) {
      this. = checkNotNull(charset);
    }
    @Override
    public Reader openStream() throws IOException {
      return new InputStreamReader(ByteSource.this.openStream(), );
    }
    @Override
    public String toString() {
      return ByteSource.this.toString() + ".asCharSource(" +  + ")";
    }
  }

  
A view of a subsection of the containing byte source.
  private final class SlicedByteSource extends ByteSource {
    private final long offset;
    private final long length;
    private SlicedByteSource(long offsetlong length) {
      checkArgument(offset >= 0, "offset (%s) may not be negative"offset);
      checkArgument(length >= 0, "length (%s) may not be negative"length);
      this. = offset;
      this. = length;
    }
    @Override
    public InputStream openStream() throws IOException {
      return sliceStream(ByteSource.this.openStream());
    }
    @Override
    public InputStream openBufferedStream() throws IOException {
      return sliceStream(ByteSource.this.openBufferedStream());
    }
    private InputStream sliceStream(InputStream inthrows IOException {
      if ( > 0) {
        try {
          ByteStreams.skipFully(in);
        } catch (Throwable e) {
          Closer closer = Closer.create();
          closer.register(in);
          try {
            throw closer.rethrow(e);
          } finally {
            closer.close();
          }
        }
      }
      return ByteStreams.limit(in);
    }
    @Override
    public ByteSource slice(long offsetlong length) {
      checkArgument(offset >= 0, "offset (%s) may not be negative"offset);
      checkArgument(length >= 0, "length (%s) may not be negative"length);
      long maxLength = this. - offset;
      return ByteSource.this.slice(this. + offset, Math.min(lengthmaxLength));
    }
    @Override
    public boolean isEmpty() throws IOException {
      return  == 0 || super.isEmpty();
    }
    @Override
    public String toString() {
      return ByteSource.this.toString() + ".slice(" +  + ", " +  + ")";
    }
  }
  private static class ByteArrayByteSource extends ByteSource {
    protected final byte[] bytes;
    protected ByteArrayByteSource(byte[] bytes) {
      this. = checkNotNull(bytes);
    }
    @Override
    public InputStream openStream() {
      return new ByteArrayInputStream();
    }
    @Override
    public InputStream openBufferedStream() throws IOException {
      return openStream();
    }
    @Override
    public boolean isEmpty() {
      return . == 0;
    }
    @Override
    public long size() {
      return .;
    }
    @Override
    public byte[] read() {
      return .clone();
    }
    @Override
    public long copyTo(OutputStream outputthrows IOException {
      output.write();
      return .;
    }
    @Override
    public <T> T read(ByteProcessor<T> processorthrows IOException {
      processor.processBytes(, 0, .);
      return processor.getResult();
    }
    @Override
    public HashCode hash(HashFunction hashFunctionthrows IOException {
      return hashFunction.hashBytes();
    }
    // TODO(user): Possibly override slice()
    @Override
    public String toString() {
      return "ByteSource.wrap("
          + Ascii.truncate(BaseEncoding.base16().encode(), 30, "...") + ")";
    }
  }
  private static final class EmptyByteSource extends ByteArrayByteSource {
    private static final EmptyByteSource INSTANCE = new EmptyByteSource();
    private EmptyByteSource() {
      super(new byte[0]);
    }
    @Override
    public CharSource asCharSource(Charset charset) {
      checkNotNull(charset);
      return CharSource.empty();
    }
    @Override
    public byte[] read() {
      return // length is 0, no need to clone
    }
    @Override
    public String toString() {
      return "ByteSource.empty()";
    }
  }
  private static final class ConcatenatedByteSource extends ByteSource {
    private final Iterable<? extends ByteSourcesources;
    ConcatenatedByteSource(Iterable<? extends ByteSourcesources) {
      this. = checkNotNull(sources);
    }
    @Override
    public InputStream openStream() throws IOException {
      return new MultiInputStream(.iterator());
    }
    @Override
    public boolean isEmpty() throws IOException {
      for (ByteSource source : ) {
        if (!source.isEmpty()) {
          return false;
        }
      }
      return true;
    }
    @Override
    public long size() throws IOException {
      long result = 0L;
      for (ByteSource source : ) {
        result += source.size();
      }
      return result;
    }
    @Override
    public String toString() {
      return "ByteSource.concat(" +  + ")";
    }
  }
New to GrepCode? Check out our FAQ X