01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 1997-1999 Raja Vallee-Rai
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the
16: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17: * Boston, MA 02111-1307, USA.
18: */
19:
20: /*
21: * Modified by the Sable Research Group and others 1997-1999.
22: * See the 'credits' file distributed with Soot for the complete list of
23: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24: */
25:
26: package soot.util;
27:
28: import java.io.*;
29:
30: /** A FilterWriter which catches to-be-escaped characters (<code>\\unnnn</code>) in the
31: * input and substitutes their escaped representation. Used for Soot output. */
32: public class EscapedWriter extends FilterWriter {
33: /** Convenience field containing the system's line separator. */
34: public final String lineSeparator = System
35: .getProperty("line.separator");
36: private final int cr = lineSeparator.charAt(0);
37: private final int lf = (lineSeparator.length() == 2) ? lineSeparator
38: .charAt(1)
39: : -1;
40:
41: /** Constructs an EscapedWriter around the given Writer. */
42: public EscapedWriter(Writer fos) {
43: super (fos);
44: }
45:
46: private final StringBuffer mini = new StringBuffer();
47:
48: /** Print a single character (unsupported). */
49: public void print(int ch) throws IOException {
50: write(ch);
51: throw new RuntimeException();
52: }
53:
54: /** Write a segment of the given String. */
55: public void write(String s, int off, int len) throws IOException {
56: for (int i = off; i < off + len; i++)
57: write(s.charAt(i));
58: }
59:
60: /** Write a single character. */
61: public void write(int ch) throws IOException {
62: if (ch >= 32 && ch <= 126 || ch == cr || ch == lf || ch == ' ') {
63: super .write(ch);
64: return;
65: }
66:
67: mini.setLength(0);
68: mini.append(Integer.toHexString(ch));
69:
70: while (mini.length() < 4)
71: mini.insert(0, "0");
72:
73: mini.insert(0, "\\u");
74: for (int i = 0; i < mini.length(); i++)
75: super.write(mini.charAt(i));
76: }
77: }
|