utilMDE
Class CountingPrintWriter

Object
  extended by Writer
      extended by PrintWriter
          extended by CountingPrintWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class CountingPrintWriter
extends PrintWriter

Prints formatted representations of objects to a text-output stream counting the number of bytes and characters printed.

Methods in this class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().


Field Summary
 
Fields inherited from class PrintWriter
out
 
Fields inherited from class Writer
lock
 
Constructor Summary
CountingPrintWriter(OutputStream out)
          Create a new PrintWriter, without automatic line flushing, from an existing OutputStream.
CountingPrintWriter(OutputStream out, boolean autoFlush)
          Create a new PrintWriter from an existing OutputStream.
CountingPrintWriter(Writer out)
          Create a new PrintWriter, without automatic line flushing.
CountingPrintWriter(Writer out, boolean autoFlush)
          Create a new PrintWriter, without automatic line flushing.
 
Method Summary
 int countBytes(char c)
          Returns the number of bytes used to represent a character.
 int countBytes(String s)
          Returns the number of bytes contained in a string.
 int getNumberOfPrintedBytes()
          Returns the total number of bytes printed using any of the 'print' or 'println' methods of this CountingPrintBuffer.
 int getNumberOfPrintedChars()
          Returns the total number of characters printed using any of the 'print' or 'println' methods of this CountingPrintBuffer.
 int getNumberOfWrittenBytes()
          Returns the total number of bytes printed using any of the 'write' methods of this CountingPrintBuffer.
 void print(boolean b)
          Print a boolean value.
 void print(char c)
          Print a character.
 void print(char[] s)
          Print an array of characters.
 void print(double d)
          Print a double-precision floating-point number.
 void print(float f)
          Print a floating-point number.
 void print(int i)
          Print an integer.
 void print(long l)
          Print a long integer.
 void print(Object obj)
          Print an object.
 void print(String s)
          Print a string.
 void println()
          Terminate the current line by writing the line separator string.
 void println(String x)
          Print a String and then terminate the line.
 void resetAll()
          Resets all the byte and char counters.
 void resetPrintedByteCounter()
          Resets printedByte counter.
 void resetPrintedCharCounter()
          Resets printedChar counter.
 void resetWrittenByteCounter()
          Resets writtenByte counter.
 void resetWrittenCharCounter()
          Resets writtenChar counter.
 void write(char[] buf)
          Write an array of characters.
 void write(char[] buf, int off, int len)
          Write a portion of a character array.
 void write(String s)
          Write a string.
 void write(String s, int off, int len)
          Write a portion of a string.
 
Methods inherited from class PrintWriter
append, append, append, checkError, clearError, close, flush, format, format, printf, printf, println, println, println, println, println, println, println, println, setError, write
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CountingPrintWriter

public CountingPrintWriter(OutputStream out)
Create a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream

CountingPrintWriter

public CountingPrintWriter(OutputStream out,
                           boolean autoFlush)
Create a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
autoFlush - A boolean; if true, the println() methods will flush the output buffer

CountingPrintWriter

public CountingPrintWriter(Writer out)
Create a new PrintWriter, without automatic line flushing.

Parameters:
out - a Writer

CountingPrintWriter

public CountingPrintWriter(Writer out,
                           boolean autoFlush)
Create a new PrintWriter, without automatic line flushing.

Parameters:
out - A writer
autoFlush - A boolean; if true, the println() methods will flush the output buffer
Method Detail

countBytes

public int countBytes(String s)
Returns the number of bytes contained in a string. If s is null, returns -1

Parameters:
s - A String

countBytes

public int countBytes(char c)
Returns the number of bytes used to represent a character.

Parameters:
c - A character.

getNumberOfPrintedBytes

public int getNumberOfPrintedBytes()
Returns the total number of bytes printed using any of the 'print' or 'println' methods of this CountingPrintBuffer.


getNumberOfWrittenBytes

public int getNumberOfWrittenBytes()
Returns the total number of bytes printed using any of the 'write' methods of this CountingPrintBuffer.


getNumberOfPrintedChars

public int getNumberOfPrintedChars()
Returns the total number of characters printed using any of the 'print' or 'println' methods of this CountingPrintBuffer.


print

public void print(String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(boolean b)
Print a boolean value. The string produced by String.valueOf(boolean) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(char c)
Print a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(char[] s)
Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(double d)
Print a double-precision floating-point number. The string produced by String.valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(float f)
Print a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(int i)
Print an integer. The string produced by String.valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

resetAll

public void resetAll()
Resets all the byte and char counters.


resetPrintedByteCounter

public void resetPrintedByteCounter()
Resets printedByte counter.


resetPrintedCharCounter

public void resetPrintedCharCounter()
Resets printedChar counter.


resetWrittenByteCounter

public void resetWrittenByteCounter()
Resets writtenByte counter.


resetWrittenCharCounter

public void resetWrittenCharCounter()
Resets writtenChar counter.


print

public void print(long l)
Print a long integer. The string produced by String.valueOf(long) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

print

public void print(Object obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Overrides:
print in class PrintWriter

println

public void println()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

When incrementing the byte count of PrintWriter, also accounts for the bytes needed to represent the line separator string.

Overrides:
println in class PrintWriter

println

public void println(String x)
Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

Overrides:
println in class PrintWriter

write

public void write(char[] buf)
Write an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.

Overrides:
write in class PrintWriter

write

public void write(char[] buf,
                  int off,
                  int len)
Write a portion of a character array.

Overrides:
write in class PrintWriter
Parameters:
buf - character array
off - Offset from which to start writing characters
len - Number of characters to write

write

public void write(String s)
Write a string.

Overrides:
write in class PrintWriter

write

public void write(String s,
                  int off,
                  int len)
Write a portion of a string.

Overrides:
write in class PrintWriter
Parameters:
s - string to be printed
off - Offset from which to start writing characters
len - Number of characters to write