public class RawText extends Sequence
Elements of the sequence are the lines of the file, as delimited by the UNIX newline character ('\n'). The file content is treated as 8 bit binary text, with no assumptions or requirements on character encoding.
Note that the first line of the file is element 0, as defined by the Sequence interface API. Traditionally in a text editor a patch file the first line is line number 1. Callers may need to subtract 1 prior to invoking methods if they are converting from "line number" to "element index".
Modifier and Type | Field and Description |
---|---|
protected byte[] |
content
The file content for this sequence.
|
static RawText |
EMPTY_TEXT
A RawText of length 0
|
protected IntList |
lines
Map of line number to starting position within
content . |
Constructor and Description |
---|
RawText(byte[] input)
Create a new sequence from an existing content byte array.
|
RawText(File file)
Create a new sequence from a file.
|
Modifier and Type | Method and Description |
---|---|
protected String |
decode(int start,
int end)
Decode a region of the text into a String.
|
String |
getLineDelimiter()
Get the line delimiter for the first line.
|
String |
getString(int i)
Get the text for a single line.
|
String |
getString(int begin,
int end,
boolean dropLF)
Get the text for a region of lines.
|
static boolean |
isBinary(byte[] raw)
Determine heuristically whether a byte array represents binary (as
opposed to text) content.
|
static boolean |
isBinary(byte[] raw,
int length)
Determine heuristically whether a byte array represents binary (as
opposed to text) content.
|
static boolean |
isBinary(InputStream raw)
Determine heuristically whether the bytes contained in a stream
represents binary (as opposed to text) content.
|
boolean |
isMissingNewlineAtEnd()
Determine if the file ends with a LF ('\n').
|
static RawText |
load(ObjectLoader ldr,
int threshold)
Read a blob object into RawText, or throw BinaryBlobException if the blob
is binary.
|
int |
size()
Get size
|
void |
writeLine(OutputStream out,
int i)
Write a specific line to the output stream, without its trailing LF.
|
public static final RawText EMPTY_TEXT
protected final byte[] content
public RawText(byte[] input)
The entire array (indexes 0 through length-1) is used as the content.
input
- the content array. The array is never modified, so passing
through cached arrays is safe.public RawText(File file) throws IOException
The entire file contents are used.
file
- the text file.IOException
- if Exceptions occur while reading the filepublic void writeLine(OutputStream out, int i) throws IOException
The specified line is copied as-is, with no character encoding translation performed.
If the specified line ends with an LF ('\n'), the LF is not copied. It is up to the caller to write the LF, if desired, between output lines.
out
- stream to copy the line data onto.i
- index of the line to extract. Note this is 0-based, so line
number 1 is actually index 0.IOException
- the stream write operation failed.public boolean isMissingNewlineAtEnd()
public String getString(int i)
i
- index of the line to extract. Note this is 0-based, so line
number 1 is actually index 0.public String getString(int begin, int end, boolean dropLF)
begin
- index of the first line to extract. Note this is 0-based, so
line number 1 is actually index 0.end
- index of one past the last line to extract.dropLF
- if true the trailing LF ('\n') of the last returned line is
dropped, if present.[begin, end)
.protected String decode(int start, int end)
start
- first byte of the content to decode.end
- one past the last byte of the content to decode.[start, end)
decoded as a String.public static boolean isBinary(byte[] raw)
raw
- the raw file content.public static boolean isBinary(InputStream raw) throws IOException
raw
- input stream containing the raw file content.IOException
- if input stream could not be readpublic static boolean isBinary(byte[] raw, int length)
raw
- the raw file content.length
- number of bytes in raw
to evaluate. This should be
raw.length
unless raw
was over-allocated by
the caller.public String getLineDelimiter()
null
public static RawText load(ObjectLoader ldr, int threshold) throws IOException, BinaryBlobException
ldr
- the ObjectLoader for the blobthreshold
- if the blob is larger than this size, it is always assumed to
be binary.BinaryBlobException
- if the blob contains binary data.IOException
- if the input could not be read.Copyright © 2017 Eclipse JGit Project. All rights reserved.