blob: 0a5706e061c7a585de5bc2fe9c1c9d6750790f95 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27/**
28 * This FilterWriter class takes an existing Writer and uses
29 * the 'back-tick U' escape notation to escape characters which are
30 * encountered within the input character based stream which
31 * are outside the 7-bit ASCII range. The native platforms linefeed
32 * character is emitted for each line of processed input
33 */
34
35package sun.tools.native2ascii;
36import java.io.*;
37import java.nio.BufferOverflowException;
38
39class N2AFilter extends FilterWriter {
40
41 public N2AFilter(Writer out) { super(out); }
42
43 public void write(char b) throws IOException {
44 char[] buf = new char[1];
45 buf[0] = (char)b;
46 write(buf, 0, 1);
47 }
48
49 public void write(char[] buf, int off, int len) throws IOException {
50
51 String lineBreak = System.getProperty("line.separator");
52
53 //System.err.println ("xx Out buffer length is " + buf.length );
54 for (int i = 0; i < len; i++) {
55 if ((buf[i] > '\u007f')) {
56 // write \udddd
57 out.write('\\');
58 out.write('u');
59 String hex =
60 Integer.toHexString(buf[i]);
61 StringBuffer hex4 = new StringBuffer(hex);
62 hex4.reverse();
63 int length = 4 - hex4.length();
64 for (int j = 0; j < length; j++) {
65 hex4.append('0');
66 }
67 for (int j = 0; j < 4; j++) {
68 out.write(hex4.charAt(3 - j));
69 }
70 } else
71 out.write(buf[i]);
72 }
73 }
74}