Package org.eclipse.jgit.util
Class IO
java.lang.Object
org.eclipse.jgit.util.IO
Input/Output utilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
read
(ReadableByteChannel channel, byte[] dst, int off, int len) Read as much of the array as possible from a channel.static final byte[]
Read an entire local file into memory as a byte array.static final byte[]
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 shorterstatic void
readFully
(InputStream fd, byte[] dst, int off, int len) Read the entire byte array into memory, or throw an exception.static String
Read the next line from a reader.Divides the given string into lines.static final byte[]
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.private static void
resetAndSkipFully
(Reader fd, long toSkip) static void
skipFully
(InputStream fd, long toSkip) Skip an entire region of an input stream.
-
Constructor Details
-
IO
private IO()
-
-
Method Details
-
readFully
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
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
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
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
andlimit()
equal to the actual length read. Callers may rely on obtaining the underlying array for efficient data access. IfsizeHint
was too large, the array may be over-allocated, resulting inlimit() < array().length
. - Throws:
IOException
- there was an error reading from the stream.
-
readFully
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
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 read
- Throws:
IOException
- there was an error reading from the stream.
-
skipFully
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
Divides the given string into lines.- Parameters:
s
- the string to read- Returns:
- the string divided into lines
- Since:
- 2.0
-
readLine
Read the next line from a reader.Like
BufferedReader.readLine()
, but only treats\n
as end-of-line, and includes the trailing newline.- Parameters:
in
- the reader to read from.sizeHint
- hint for buffer sizing; 0 or negative for default.- Returns:
- the next line from the input, always ending in
\n
unless EOF was reached. - Throws:
IOException
- there was an error reading from the stream.- Since:
- 4.1
-
resetAndSkipFully
- Throws:
IOException
-