KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > EscapedWriter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * 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
27 package soot.util;
28
29 import java.io.*;
30
31 /** A FilterWriter which catches to-be-escaped characters (<code>\\unnnn</code>) in the
32  * input and substitutes their escaped representation. Used for Soot output. */

33 public class EscapedWriter extends FilterWriter
34 {
35     /** Convenience field containing the system's line separator. */
36     public final String JavaDoc lineSeparator = System.getProperty("line.separator");
37     private final int cr = lineSeparator.charAt(0);
38     private final int lf = (lineSeparator.length() == 2) ? lineSeparator.charAt(1) : -1;
39
40     /** Constructs an EscapedWriter around the given Writer. */
41     public EscapedWriter(Writer fos)
42     {
43         super(fos);
44     }
45
46     private final StringBuffer JavaDoc mini = new StringBuffer JavaDoc();
47
48     /** Print a single character (unsupported). */
49     public void print(int ch) throws IOException
50     {
51         write(ch);
52         throw new RuntimeException JavaDoc();
53     }
54   
55     /** Write a segment of the given String. */
56     public void write(String JavaDoc s, int off, int len) throws IOException
57     {
58         for(int i = off; i < off + len; i++)
59             write(s.charAt(i));
60     }
61   
62     /** Write a single character. */
63     public void write(int ch) throws IOException
64     {
65         if (ch >= 32 && ch <= 126 || ch == cr || ch == lf || ch == ' ')
66             { super.write(ch); return; }
67         
68         mini.setLength(0);
69         mini.append(Integer.toHexString(ch));
70
71         while (mini.length() < 4)
72             mini.insert(0, "0");
73
74         mini.insert(0, "\\u");
75         for (int i = 0; i < mini.length(); i++)
76             super.write(mini.charAt(i));
77     }
78 }
79
Popular Tags