1 /*
 2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. 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.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.asmtools.jdis;
24 
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.Writer;
28 
29 /**
30  *
31  */
32 public class uEscWriter extends Writer {
33     /*-------------------------------------------------------- */
34     /* uEscWriter Fields */
35 
36     static final char[] hexTable = "0123456789ABCDEF".toCharArray();
37     OutputStream out;
38     byte[] tmpl;
39     /*-------------------------------------------------------- */
40 
41     public uEscWriter(OutputStream out) {
42         this.out = out;
43         tmpl = new byte[6];
44         tmpl[0] = (byte) '\\';
45         tmpl[1] = (byte) 'u';
46     }
47 
48     @Override
49     public synchronized void write(int c) throws IOException {
50         if (c < 128) {
51             out.write(c);
52             return;
53         }
54         // write \udddd
55         byte[] tmpll = tmpl;
56         for (int k = 3; k >= 0; k--) {
57             tmpll[5 - k] = (byte) hexTable[(c >> 4 * k) & 0xF];
58         }
59         out.write(tmpll, 0, 6);
60     }
61 
62     @Override
63     public synchronized void write(char[] cc, int ofs, int len) throws IOException {
64         for (int k = ofs; k < len; k++) {
65             write(cc[k]);
66         }
67     }
68 
69     @Override
70     public void flush() {
71     }
72 
73     @Override
74     public void close() {
75     }
76 } // end uEscWriter
77