1

Previously, I had a code such as this:

        Writer writer = new OutputStreamWriter(System.out, "UTF-8");
        generator.stream(writer);

Now, my code looks like this:

            string = new StringBuilder();
            OutputStream output = new OutputStream()
            {
                public String getString()
                {
                    return string.toString();
                }
                @Override
                public void write(int b) throws IOException {
                if ((char)b != '\n')
                {
                    string.append((char) b );
                }
            }

            public String toString(){
                return string.toString();
            }
        };

        Writer writer = new OutputStreamWriter(output, "UTF-8");
        generator.stream(writer);

        writer.close();
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        ps.println(string.toString());

The problem with the first code was that it didn't guarantee that the output is in a single row. In the other hand, my new code resolves this issue, however, a new problem has appeared: Instead of 'Á' the output sends 'ᅢチ', which is quite painful, as Hungarian and Polish characters should be supported. For non-Hungarian, non-Polish characters, my current output is correct, but the special characters in the written Hungarian and Polish language are not handled by my new code. How can I overcome this issue?

Thanks.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    What is `generator.stream()`? – SLaks Mar 07 '13 at 03:27
  • generator is an SVGGraphics2D object, stream() is its stream method, more details here: http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/svggen/SVGGraphics2D.html – Lajos Arpad Mar 07 '13 at 03:42

2 Answers2

3

Extend the OutputStreamWriter class instead and override the appropriate method(s) to remove the newlines.

public class NoNewLineOutputStreamWriter extends OutputStreamWriter {

    public NoNewLineOutputStreamWriter(OutputStream out,String charset) throws UnsupportedEncodingException {
        super(out,charset);
    }

    public void write(String str) throws IOException {
        super.write(str.replaceAll("\n", ""));
    };

    public void write(int c) {
        if (c == '\n')
            return;
        else
            super.write(c);
    }

}

Then call your writer as before

    Writer writer = new NoNewLineOutputStreamWriter(System.out, "UTF-8");
    generator.stream(writer);
scott
  • 974
  • 7
  • 10
0

This must be because of the OS's default char set.
I had the similar issue regarding é converting into é.
I changed the JVM file.encoding to UTF as -Dfile.encoding=UTF-8.
Then my problem was solved.

Read this as well :

Community
  • 1
  • 1
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • Thank you, but my system encoding is UTF-8. I believe I am mishandling the variable called output, as the object called string already has this flaw with Hungarian and Polish characters. – Lajos Arpad Mar 07 '13 at 04:31