org.eclipse.jgit.util
Class IO

java.lang.Object
  extended by org.eclipse.jgit.util.IO

public class IO
extends Object

Input/Output utilities


Method Summary
static int read(ReadableByteChannel channel, byte[] dst, int off, int len)
          Read as much of the array as possible from a channel.
static byte[] readFully(File path)
          Read an entire local file into memory as a byte array.
static byte[] readFully(File path, int max)
          Read an entire local file into memory as a byte array.
static int readFully(InputStream fd, byte[] dst, int off)
          Read the entire byte array into memory, unless input is shorter
static void readFully(InputStream fd, byte[] dst, int off, int len)
          Read the entire byte array into memory, or throw an exception.
static List<String> readLines(String s)
          Divides the given string into lines.
static byte[] readSome(File path, int limit)
          Read at most limit bytes from the local file into memory as a byte array.
static ByteBuffer readWholeStream(InputStream in, int sizeHint)
          Read an entire input stream into memory as a ByteBuffer.
static void skipFully(InputStream fd, long toSkip)
          Skip an entire region of an input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readFully

public static final byte[] readFully(File path)
                              throws FileNotFoundException,
                                     IOException
Read an entire local file into memory as a byte array.

Parameters:
path - location of the file to read.
Returns:
complete contents of the requested local file.
Throws:
FileNotFoundException - the file does not exist.
IOException - the file exists, but its contents cannot be read.

readSome

public static final byte[] readSome(File path,
                                    int limit)
                             throws FileNotFoundException,
                                    IOException
Read at most limit bytes from the local file into memory as a byte array.

Parameters:
path - location of the file to read.
limit - maximum number of bytes to read, if the file is larger than only the first limit number of bytes are returned
Returns:
complete contents of the requested local file. If the contents exceeds the limit, then only the limit is returned.
Throws:
FileNotFoundException - the file does not exist.
IOException - the file exists, but its contents cannot be read.

readFully

public static final byte[] readFully(File path,
                                     int max)
                              throws FileNotFoundException,
                                     IOException
Read an entire local file into memory as a byte array.

Parameters:
path - location of the file to read.
max - maximum number of bytes to read, if the file is larger than this limit an IOException is thrown.
Returns:
complete contents of the requested local file.
Throws:
FileNotFoundException - the file does not exist.
IOException - the file exists, but its contents cannot be read.

readWholeStream

public static ByteBuffer readWholeStream(InputStream in,
                                         int sizeHint)
                                  throws IOException
Read an entire input stream into memory as a ByteBuffer. Note: The stream is read to its end and is not usable after calling this method. The caller is responsible for closing the stream.

Parameters:
in - input stream to be read.
sizeHint - a hint on the approximate number of bytes contained in the stream, used to allocate temporary buffers more efficiently
Returns:
complete contents of the input stream. The ByteBuffer always has a writable backing array, with position() == 0 and limit() equal to the actual length read. Callers may rely on obtaining the underlying array for efficient data access. If sizeHint was too large, the array may be over-allocated, resulting in limit() < array().length.
Throws:
IOException - there was an error reading from the stream.

readFully

public static void readFully(InputStream fd,
                             byte[] dst,
                             int off,
                             int len)
                      throws IOException
Read the entire byte array into memory, or throw an exception.

Parameters:
fd - input stream to read the data from.
dst - buffer that must be fully populated, [off, off+len).
off - position within the buffer to start writing to.
len - number of bytes that must be read.
Throws:
EOFException - the stream ended before dst was fully populated.
IOException - there was an error reading from the stream.

read

public static int read(ReadableByteChannel channel,
                       byte[] dst,
                       int off,
                       int len)
                throws IOException
Read as much of the array as possible from a channel.

Parameters:
channel - channel to read data from.
dst - buffer that must be fully populated, [off, off+len).
off - position within the buffer to start writing to.
len - number of bytes that should be read.
Returns:
number of bytes actually read.
Throws:
IOException - there was an error reading from the channel.

readFully

public static int readFully(InputStream fd,
                            byte[] dst,
                            int off)
                     throws IOException
Read the entire byte array into memory, unless input is shorter

Parameters:
fd - input stream to read the data from.
dst - buffer that must be fully populated, [off, off+len).
off - position within the buffer to start writing to.
Returns:
number of bytes in buffer or stream, whichever is shortest
Throws:
IOException - there was an error reading from the stream.

skipFully

public static void skipFully(InputStream fd,
                             long toSkip)
                      throws IOException
Skip an entire region of an input stream.

The input stream's position is moved forward by the number of requested bytes, discarding them from the input. This method does not return until the exact number of bytes requested has been skipped.

Parameters:
fd - the stream to skip bytes from.
toSkip - total number of bytes to be discarded. Must be >= 0.
Throws:
EOFException - the stream ended before the requested number of bytes were skipped.
IOException - there was an error reading from the stream.

readLines

public static List<String> readLines(String s)
Divides the given string into lines.

Parameters:
s - the string to read
Returns:
the string divided into lines
Since:
2.0


Copyright © 2012. All Rights Reserved.