LATEST SSCCEE
Why does example below output different strings?
package tests.java;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Arrays;
public class Try_PrintWriterEncoding3 {
public static void main(String[] args) {
final PrintStream oldOut = System.out;
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b}, Charset.defaultCharset()));
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
oldOut.print(new String(Arrays.copyOf(b, len), Charset.defaultCharset()));
}
@Override
public void write(byte[] b) throws IOException {
throw new UnsupportedOperationException();
}
}));
System.out.println("Привет, мир!");
}
}
PREVIOUS EXAMPLES
I would like to write custom stdout stream, but fails with international encoding.
It is told, that PrintStream converts characters to bytes according to default encoding. This could mean that to decode one should use default encoding too.
But it doesn't work.
Also any other possible encodings don't work.
package tests.java;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
public class Try_PrintWriterEncoding {
public static void main(String[] args) {
final PrintStream oldOut = System.out;
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.write(b); // works
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new char[] {(char)b})); // does not work (garbage type 1)
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b})); // does not work (garbage type 2)
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b}, Charset.defaultCharset())); // does not work (garbage type 2)
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b}, Charset.forName("UTF-8"))); // does not work (garbage type 2)
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b}, Charset.forName("CP866"))); // does not work (garbage type 3)
}
}));
System.out.println("Привет, мир!");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
oldOut.print(new String(new byte[] {(byte)b}, Charset.forName("Cp1251"))); // does not work (garbage type 4)
}
}));
System.out.println("Привет, мир!");
}
}
OUTPUT