java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
com.ibm.jzos.TranscodingPrintStream
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
A subclass of PrintStream that sets an encoding on its OutputStreamWriter
and also transcodes any write(byte[]) messages that are sent to it
when it is treated as an OutputStream. This avoids the problem caused when
an application wraps System.out or System.err in its own PrintWriter which
would override and lose the PrintStream's encoding.
When both source and target codepages are single byte encodings, (e.g. ISO-8859-1, IBM-1047)
a direct map can be used, and is the most efficient way to transcode. If one
or both of the codepages are multi-byte (e.g. UTF-8) the CharsetEncoder and
CharsetDecoder classes need to be used. For this case, the Transcoder class
is used. It provides a protocol that accepts an array of source codepage encoded bytes,
and writes the target codepage encoded bytes to a supplied output stream.
-
Constructor Summary
ConstructorsConstructorDescriptionTranscodingPrintStream(OutputStream out, boolean autoFlush, String targetEncoding, boolean enable) Create a new transcoding print stream.TranscodingPrintStream(OutputStream out, boolean autoFlush, String sourceEncoding, String targetEncoding, boolean enable) Create a new transcoding print stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidflush()Overridden to flush the transcoder if one is being used.byte[]Answer the translate table in place.booleanisActive()Answer whether this PrintStream is actively transcoding.voidwrite(byte[] buf, int off, int len) OverridePrintStream.write(byte[], int, int)to transcode bytes from the default encoding to the target encoding.voidwrite(int b) OverridePrintStream.write(int b)to transcode bytes from the default encoding to the target encoding.Methods inherited from class java.io.PrintStream
append, append, append, checkError, close, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, write, writeBytesMethods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
TranscodingPrintStream
public TranscodingPrintStream(OutputStream out, boolean autoFlush, String targetEncoding, boolean enable) throws UnsupportedEncodingException Create a new transcoding print stream.- Parameters:
out- The output stream to which values and objects will be printedautoFlush- A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of theprintlnmethods is invoked, or a newline character or byte ('\n') is writtentargetEncoding- The String naming the encoding to use, both for the internal OutputStreamWriter and for translating write(byte[]) IOStream messages sent to this object- Throws:
UnsupportedEncodingException- See Also:
-
TranscodingPrintStream
public TranscodingPrintStream(OutputStream out, boolean autoFlush, String sourceEncoding, String targetEncoding, boolean enable) throws UnsupportedEncodingException Create a new transcoding print stream.- Parameters:
out- The output stream to which values and objects will be printedautoFlush- A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of theprintlnmethods is invoked, or a newline character or byte ('\n') is writtensourceEncoding- The String naming the encoding that is the source encoding for the transcoder. If null, the default will be used (the "file.encoding" System property)targetEncoding- The String naming the encoding to use, both for the internal OutputStreamWriter and for translating write(byte[]) IOStream messages sent to this object- Throws:
UnsupportedEncodingException- See Also:
-
-
Method Details
-
write
public void write(int b) OverridePrintStream.write(int b)to transcode bytes from the default encoding to the target encoding.- Overrides:
writein classPrintStream
-
write
public void write(byte[] buf, int off, int len) OverridePrintStream.write(byte[], int, int)to transcode bytes from the default encoding to the target encoding.- Overrides:
writein classPrintStream
-
isActive
public boolean isActive()Answer whether this PrintStream is actively transcoding. If false, the write() methods default to the superclass behavior. -
getTranslateTable
public byte[] getTranslateTable()Answer the translate table in place. Will return null if !isActive() or either encoding is multi-byte. -
flush
public void flush()Overridden to flush the transcoder if one is being used.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classPrintStream
-