jdk/src/jdk.dev/share/classes/sun/tools/native2ascii/A2NFilter.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 25859 3317bb8137f4
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * This FilterReader class processes a sequence of characters from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * a source stream containing a mixture of 7-bit ASCII data and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * 'back-tick U' escaped sequences representing characters which have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * the possibility of being encoded in a user specified encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The filter relies on knowing the target encoding and makes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * determination as to whether a given supplied character in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * source character stream is encodeable in the target encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * If not, it is remains in its back-tick U escaped form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
package sun.tools.native2ascii;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class A2NFilter extends FilterReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // maintain a trailing buffer to hold any incompleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // unicode escaped sequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private char[] trailChars = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public A2NFilter(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public int read(char[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int numChars = 0;        // how many characters have been read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int retChars = 0;        // how many characters we'll return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        char[] cBuf = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int cOffset = 0;         // offset at which we'll start reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // copy trailing chars from previous invocation to input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (trailChars != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            for (int i = 0; i < trailChars.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                cBuf[i] = trailChars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            numChars = trailChars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            trailChars = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int n = in.read(cBuf, numChars, len - numChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (numChars == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                return -1;              // EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            numChars += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (int i = 0; i < numChars;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            char c = cBuf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (c != '\\' || (eof && numChars <= 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                // Not a backslash, so copy and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // Always pass non backslash chars straight thru
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                // for regular encoding. If backslash occurs in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // input stream at the final 5 chars then don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                // attempt to read-ahead and de-escape since these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // are literal occurrences of U+005C which need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                // be encoded verbatim in the target encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                buf[retChars++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int remaining = numChars - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (remaining < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // Might be the first character of a unicode escape, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                // don't have enough characters to tell, so save it and finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                trailChars = new char[1 + remaining];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                trailChars[0] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                for (int j = 0; j < remaining; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    trailChars[1 + j] = cBuf[i + j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // At this point we have at least five characters remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            c = cBuf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (c != 'u') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // Not a unicode escape, so copy and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                buf[retChars++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                buf[retChars++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // The next four characters are the hex part of a unicode escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            char rc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            boolean isUE = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                rc = (char)Integer.parseInt(new String(cBuf, i, 4), 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                isUE = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (isUE && Main.canConvert(rc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // We'll be able to convert this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                buf[retChars++] = rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                i += 4; // Align beyond the current uXXXX sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // We won't, so just retain the original sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                buf[retChars++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                buf[retChars++] = 'u';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return retChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        char[] buf = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (read(buf, 0, 1) == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return (int)buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}