Constructors Summary |
---|
public FastPrintWriter(OutputStream out)Constructs a new {@code PrintWriter} with {@code out} as its target
stream. By default, the new print writer does not automatically flush its
contents to the target stream when a newline is encountered.
this(out, false, 8192);
|
public FastPrintWriter(OutputStream out, boolean autoFlush)Constructs a new {@code PrintWriter} with {@code out} as its target
stream. The parameter {@code autoFlush} determines if the print writer
automatically flushes its contents to the target stream when a newline is
encountered.
this(out, autoFlush, 8192);
|
public FastPrintWriter(OutputStream out, boolean autoFlush, int bufferLen)Constructs a new {@code PrintWriter} with {@code out} as its target
stream and a custom buffer size. The parameter {@code autoFlush} determines
if the print writer automatically flushes its contents to the target stream
when a newline is encountered.
super(new DummyWriter(), autoFlush);
if (out == null) {
throw new NullPointerException("out is null");
}
mBufferLen = bufferLen;
mText = new char[bufferLen];
mBytes = ByteBuffer.allocate(mBufferLen);
mOutputStream = out;
mWriter = null;
mPrinter = null;
mAutoFlush = autoFlush;
mSeparator = System.lineSeparator();
initDefaultEncoder();
|
public FastPrintWriter(Writer wr)Constructs a new {@code PrintWriter} with {@code wr} as its target
writer. By default, the new print writer does not automatically flush its
contents to the target writer when a newline is encountered.
NOTE: Unlike PrintWriter, this version will still do buffering inside of
FastPrintWriter before sending data to the Writer. This means you must call
flush() before retrieving any data from the Writer.
this(wr, false, 8192);
|
public FastPrintWriter(Writer wr, boolean autoFlush)Constructs a new {@code PrintWriter} with {@code wr} as its target
writer. The parameter {@code autoFlush} determines if the print writer
automatically flushes its contents to the target writer when a newline is
encountered.
this(wr, autoFlush, 8192);
|
public FastPrintWriter(Writer wr, boolean autoFlush, int bufferLen)Constructs a new {@code PrintWriter} with {@code wr} as its target
writer and a custom buffer size. The parameter {@code autoFlush} determines
if the print writer automatically flushes its contents to the target writer
when a newline is encountered.
super(new DummyWriter(), autoFlush);
if (wr == null) {
throw new NullPointerException("wr is null");
}
mBufferLen = bufferLen;
mText = new char[bufferLen];
mBytes = null;
mOutputStream = null;
mWriter = wr;
mPrinter = null;
mAutoFlush = autoFlush;
mSeparator = System.lineSeparator();
initDefaultEncoder();
|
public FastPrintWriter(android.util.Printer pr)Constructs a new {@code PrintWriter} with {@code pr} as its target
printer and the default buffer size. Because a {@link Printer} is line-base,
autoflush is always enabled.
this(pr, 512);
|
public FastPrintWriter(android.util.Printer pr, int bufferLen)Constructs a new {@code PrintWriter} with {@code pr} as its target
printer and a custom buffer size. Because a {@link Printer} is line-base,
autoflush is always enabled.
super(new DummyWriter(), true);
if (pr == null) {
throw new NullPointerException("pr is null");
}
mBufferLen = bufferLen;
mText = new char[bufferLen];
mBytes = null;
mOutputStream = null;
mWriter = null;
mPrinter = pr;
mAutoFlush = true;
mSeparator = System.lineSeparator();
initDefaultEncoder();
|
Methods Summary |
---|
public java.io.PrintWriter | append(java.lang.CharSequence csq, int start, int end)Appends a subsequence of the character sequence {@code csq} to the
target. This method works the same way as {@code
PrintWriter.print(csq.subsequence(start, end).toString())}. If {@code
csq} is {@code null}, then the specified subsequence of the string "null"
will be written to the target.
if (csq == null) {
csq = "null";
}
String output = csq.subSequence(start, end).toString();
write(output, 0, output.length());
return this;
|
private void | appendLocked(char c)
int pos = mPos;
if (pos >= (mBufferLen-1)) {
flushLocked();
pos = mPos;
}
mText[pos] = c;
mPos = pos+1;
|
private void | appendLocked(java.lang.String str, int i, int length)
final int BUFFER_LEN = mBufferLen;
if (length > BUFFER_LEN) {
final int end = i + length;
while (i < end) {
int next = i + BUFFER_LEN;
appendLocked(str, i, next < end ? BUFFER_LEN : (end - i));
i = next;
}
return;
}
int pos = mPos;
if ((pos+length) > BUFFER_LEN) {
flushLocked();
pos = mPos;
}
str.getChars(i, i + length, mText, pos);
mPos = pos + length;
|
private void | appendLocked(char[] buf, int i, int length)
final int BUFFER_LEN = mBufferLen;
if (length > BUFFER_LEN) {
final int end = i + length;
while (i < end) {
int next = i + BUFFER_LEN;
appendLocked(buf, i, next < end ? BUFFER_LEN : (end - i));
i = next;
}
return;
}
int pos = mPos;
if ((pos+length) > BUFFER_LEN) {
flushLocked();
pos = mPos;
}
System.arraycopy(buf, i, mText, pos, length);
mPos = pos + length;
|
public boolean | checkError()Flushes this writer and returns the value of the error flag.
flush();
synchronized (lock) {
return mIoError;
}
|
protected void | clearError()Sets the error state of the stream to false.
synchronized (lock) {
mIoError = false;
}
|
public void | close()
synchronized (lock) {
try {
flushLocked();
if (mOutputStream != null) {
mOutputStream.close();
} else if (mWriter != null) {
mWriter.close();
}
} catch (IOException e) {
setError();
}
}
|
public void | flush()Ensures that all pending data is sent out to the target. It also
flushes the target. If an I/O error occurs, this writer's error
state is set to {@code true}.
synchronized (lock) {
try {
flushLocked();
if (mOutputStream != null) {
mOutputStream.flush();
} else if (mWriter != null) {
mWriter.flush();
}
} catch (IOException e) {
setError();
}
}
|
private void | flushBytesLocked()
int position;
if ((position = mBytes.position()) > 0) {
mBytes.flip();
mOutputStream.write(mBytes.array(), 0, position);
mBytes.clear();
}
|
private void | flushLocked()
//Log.i("PackageManager", "flush mPos=" + mPos);
if (mPos > 0) {
if (mOutputStream != null) {
CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
CoderResult result = mCharset.encode(charBuffer, mBytes, true);
while (true) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
flushBytesLocked();
result = mCharset.encode(charBuffer, mBytes, true);
continue;
}
break;
}
flushBytesLocked();
mOutputStream.flush();
} else if (mWriter != null) {
mWriter.write(mText, 0, mPos);
mWriter.flush();
} else {
int nonEolOff = 0;
final int sepLen = mSeparator.length();
final int len = sepLen < mPos ? sepLen : mPos;
while (nonEolOff < len && mText[mPos-1-nonEolOff]
== mSeparator.charAt(mSeparator.length()-1-nonEolOff)) {
nonEolOff++;
}
if (nonEolOff >= mPos) {
mPrinter.println("");
} else {
mPrinter.println(new String(mText, 0, mPos-nonEolOff));
}
}
mPos = 0;
}
|
private final void | initDefaultEncoder()
mCharset = Charset.defaultCharset().newEncoder();
mCharset.onMalformedInput(CodingErrorAction.REPLACE);
mCharset.onUnmappableCharacter(CodingErrorAction.REPLACE);
|
private final void | initEncoder(java.lang.String csn)
try {
mCharset = Charset.forName(csn).newEncoder();
} catch (Exception e) {
throw new UnsupportedEncodingException(csn);
}
mCharset.onMalformedInput(CodingErrorAction.REPLACE);
mCharset.onUnmappableCharacter(CodingErrorAction.REPLACE);
|
public void | print(char[] charArray)Prints the string representation of the specified character array
to the target.
synchronized (lock) {
try {
appendLocked(charArray, 0, charArray.length);
} catch (IOException e) {
}
}
|
public void | print(char ch)Prints the string representation of the specified character to the
target.
synchronized (lock) {
try {
appendLocked(ch);
} catch (IOException e) {
}
}
|
public void | print(java.lang.String str)Prints a string to the target. The string is converted to an array of
bytes using the encoding chosen during the construction of this writer.
The bytes are then written to the target with {@code write(int)}.
If an I/O error occurs, this writer's error flag is set to {@code true}.
if (str == null) {
str = String.valueOf((Object) null);
}
synchronized (lock) {
try {
appendLocked(str, 0, str.length());
} catch (IOException e) {
setError();
}
}
|
public void | print(int inum)
if (inum == 0) {
print("0");
} else {
super.print(inum);
}
|
public void | print(long lnum)
if (lnum == 0) {
print("0");
} else {
super.print(lnum);
}
|
public void | println()Prints a newline. Flushes this writer if the autoFlush flag is set to {@code true}.
synchronized (lock) {
try {
appendLocked(mSeparator, 0, mSeparator.length());
if (mAutoFlush) {
flushLocked();
}
} catch (IOException e) {
setError();
}
}
|
public void | println(int inum)
if (inum == 0) {
println("0");
} else {
super.println(inum);
}
|
public void | println(long lnum)
if (lnum == 0) {
println("0");
} else {
super.println(lnum);
}
|
public void | println(char[] chars)Prints the string representation of the character array {@code chars} followed by a newline.
Flushes this writer if the autoFlush flag is set to {@code true}.
print(chars);
println();
|
public void | println(char c)Prints the string representation of the char {@code c} followed by a newline.
Flushes this writer if the autoFlush flag is set to {@code true}.
print(c);
println();
|
protected void | setError()Sets the error flag of this writer to true.
synchronized (lock) {
mIoError = true;
}
|
public void | write(char[] buf, int offset, int count)Writes {@code count} characters from {@code buffer} starting at {@code
offset} to the target.
This writer's error flag is set to {@code true} if this writer is closed
or an I/O error occurs.
synchronized (lock) {
try {
appendLocked(buf, offset, count);
} catch (IOException e) {
}
}
|
public void | write(int oneChar)Writes one character to the target. Only the two least significant bytes
of the integer {@code oneChar} are written.
This writer's error flag is set to {@code true} if this writer is closed
or an I/O error occurs.
synchronized (lock) {
try {
appendLocked((char) oneChar);
} catch (IOException e) {
}
}
|
public void | write(java.lang.String str)Writes the characters from the specified string to the target.
synchronized (lock) {
try {
appendLocked(str, 0, str.length());
} catch (IOException e) {
}
}
|
public void | write(java.lang.String str, int offset, int count)Writes {@code count} characters from {@code str} starting at {@code
offset} to the target.
synchronized (lock) {
try {
appendLocked(str, offset, count);
} catch (IOException e) {
}
}
|