src/java.base/share/classes/sun/security/util/HexDumpEncoder.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 34687 jdk/src/java.base/share/classes/sun/security/util/HexDumpEncoder.java@d302ed125dc9
child 59024 b046ba510bbc
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
     2
 * Copyright (c) 1995, 2015, 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
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    27
package sun.security.util;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    28
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    29
import java.io.ByteArrayInputStream;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    30
import java.io.ByteArrayOutputStream;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    31
import java.io.InputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    35
import java.nio.ByteBuffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class encodes a buffer into the classic: "Hexadecimal Dump" format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the past. It is useful for analyzing the contents of binary buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The format produced is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * xxxx: 00 11 22 33 44 55 66 77   88 99 aa bb cc dd ee ff ................
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Where xxxx is the offset into the buffer in 16 byte chunks, followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * by ascii coded hexadecimal bytes followed by the ASCII representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the bytes or '.' if they are not valid bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      Chuck McManis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    51
public class HexDumpEncoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int thisLineLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int currentByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte thisLine[] = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void hexDigit(PrintStream p, byte x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        c = (char) ((x >> 4) & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (c > 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            c = (char) ((c-10) + 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            c = (char)(c + '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        p.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        c = (char) (x & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (c > 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            c = (char)((c-10) + 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            c = (char)(c + '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        p.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected int bytesPerAtom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected int bytesPerLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return (16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected void encodeBufferPrefix(OutputStream o) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        offset = 0;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    85
        pStream = new PrintStream(o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected void encodeLinePrefix(OutputStream o, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        hexDigit(pStream, (byte)((offset >>> 8) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        hexDigit(pStream, (byte)(offset & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        pStream.print(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        currentByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        thisLineLength = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected void encodeAtom(OutputStream o, byte buf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        thisLine[currentByte] = buf[off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        hexDigit(pStream, buf[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        pStream.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        currentByte++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (currentByte == 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            pStream.print("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void encodeLineSuffix(OutputStream o) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (thisLineLength < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (int i = thisLineLength; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                pStream.print("   ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (i == 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    pStream.print("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        pStream.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = 0; i < thisLineLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                pStream.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                pStream.write(thisLine[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        pStream.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        offset += thisLineLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   125
    /** Stream that understands "printing" */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   126
    protected PrintStream pStream;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   127
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   128
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   129
     * This method works around the bizarre semantics of BufferedInputStream's
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   130
     * read method.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   131
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   132
    protected int readFully(InputStream in, byte buffer[])
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   133
            throws java.io.IOException {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   134
        for (int i = 0; i < buffer.length; i++) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   135
            int q = in.read();
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   136
            if (q == -1)
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   137
                return i;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   138
            buffer[i] = (byte)q;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   139
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   140
        return buffer.length;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   141
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   142
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   143
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   144
     * Encode bytes from the input stream, and write them as text characters
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   145
     * to the output stream. This method will run until it exhausts the
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   146
     * input stream, but does not print the line suffix for a final
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   147
     * line that is shorter than bytesPerLine().
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   148
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   149
    public void encode(InputStream inStream, OutputStream outStream)
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   150
        throws IOException
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   151
    {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   152
        int     j;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   153
        int     numBytes;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   154
        byte    tmpbuffer[] = new byte[bytesPerLine()];
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   155
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   156
        encodeBufferPrefix(outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   157
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   158
        while (true) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   159
            numBytes = readFully(inStream, tmpbuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   160
            if (numBytes == 0) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   161
                break;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   162
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   163
            encodeLinePrefix(outStream, numBytes);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   164
            for (j = 0; j < numBytes; j += bytesPerAtom()) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   165
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   166
                if ((j + bytesPerAtom()) <= numBytes) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   167
                    encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   168
                } else {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   169
                    encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   170
                }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   171
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   172
            if (numBytes < bytesPerLine()) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   173
                break;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   174
            } else {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   175
                encodeLineSuffix(outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   176
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   177
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   178
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   179
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   180
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   181
     * A 'streamless' version of encode that simply takes a buffer of
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   182
     * bytes and returns a string containing the encoded buffer.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   183
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   184
    public String encode(byte aBuffer[]) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   185
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   186
        ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   187
        String retVal = null;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   188
        try {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   189
            encode(inStream, outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   190
            // explicit ascii->unicode conversion
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   191
            retVal = outStream.toString("ISO-8859-1");
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   192
        } catch (Exception IOException) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   193
            // This should never happen.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   194
            throw new Error("CharacterEncoder.encode internal error");
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   195
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   196
        return (retVal);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   197
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   198
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   199
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   200
     * Return a byte array from the remaining bytes in this ByteBuffer.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   201
     * <P>
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   202
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   203
     * <P>
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   204
     * To avoid an extra copy, the implementation will attempt to return the
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   205
     * byte array backing the ByteBuffer.  If this is not possible, a
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   206
     * new byte array will be created.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   207
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   208
    private byte [] getBytes(ByteBuffer bb) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   209
        /*
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   210
         * This should never return a BufferOverflowException, as we're
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   211
         * careful to allocate just the right amount.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   212
         */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   213
        byte [] buf = null;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   214
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   215
        /*
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   216
         * If it has a usable backing byte buffer, use it.  Use only
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   217
         * if the array exactly represents the current ByteBuffer.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   218
         */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   219
        if (bb.hasArray()) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   220
            byte [] tmp = bb.array();
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   221
            if ((tmp.length == bb.capacity()) &&
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   222
                    (tmp.length == bb.remaining())) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   223
                buf = tmp;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   224
                bb.position(bb.limit());
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   225
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   226
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   227
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   228
        if (buf == null) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   229
            /*
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   230
             * This class doesn't have a concept of encode(buf, len, off),
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   231
             * so if we have a partial buffer, we must reallocate
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   232
             * space.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   233
             */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   234
            buf = new byte[bb.remaining()];
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   235
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   236
            /*
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   237
             * position() automatically updated
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   238
             */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   239
            bb.get(buf);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   240
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   241
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   242
        return buf;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   243
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   244
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   245
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   246
     * A 'streamless' version of encode that simply takes a ByteBuffer
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   247
     * and returns a string containing the encoded buffer.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   248
     * <P>
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   249
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   250
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   251
    public String encode(ByteBuffer aBuffer) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   252
        byte [] buf = getBytes(aBuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   253
        return encode(buf);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   254
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   255
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   256
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   257
     * Encode bytes from the input stream, and write them as text characters
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   258
     * to the output stream. This method will run until it exhausts the
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   259
     * input stream. It differs from encode in that it will add the
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   260
     * line at the end of a final line that is shorter than bytesPerLine().
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   261
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   262
    public void encodeBuffer(InputStream inStream, OutputStream outStream)
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   263
        throws IOException
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   264
    {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   265
        int     j;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   266
        int     numBytes;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   267
        byte    tmpbuffer[] = new byte[bytesPerLine()];
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   268
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   269
        encodeBufferPrefix(outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   270
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   271
        while (true) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   272
            numBytes = readFully(inStream, tmpbuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   273
            if (numBytes == 0) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   274
                break;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   275
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   276
            encodeLinePrefix(outStream, numBytes);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   277
            for (j = 0; j < numBytes; j += bytesPerAtom()) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   278
                if ((j + bytesPerAtom()) <= numBytes) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   279
                    encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   280
                } else {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   281
                    encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   282
                }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   283
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   284
            encodeLineSuffix(outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   285
            if (numBytes < bytesPerLine()) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   286
                break;
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   287
            }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   288
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   289
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   290
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   291
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   292
     * Encode the buffer in <i>aBuffer</i> and write the encoded
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   293
     * result to the OutputStream <i>aStream</i>.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   294
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   295
    public void encodeBuffer(byte aBuffer[], OutputStream aStream)
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   296
        throws IOException
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   297
    {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   298
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   299
        encodeBuffer(inStream, aStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   300
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   301
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   302
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   303
     * A 'streamless' version of encode that simply takes a buffer of
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   304
     * bytes and returns a string containing the encoded buffer.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   305
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   306
    public String encodeBuffer(byte aBuffer[]) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   307
        ByteArrayOutputStream   outStream = new ByteArrayOutputStream();
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   308
        ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   309
        try {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   310
            encodeBuffer(inStream, outStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   311
        } catch (Exception IOException) {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   312
            // This should never happen.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   313
            throw new Error("CharacterEncoder.encodeBuffer internal error");
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   314
        }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   315
        return (outStream.toString());
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   316
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   317
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   318
    /**
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   319
     * Encode the <i>aBuffer</i> ByteBuffer and write the encoded
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   320
     * result to the OutputStream <i>aStream</i>.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   321
     * <P>
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   322
     * The ByteBuffer's position will be advanced to ByteBuffer's limit.
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   323
     */
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   324
    public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   325
        throws IOException
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   326
    {
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   327
        byte [] buf = getBytes(aBuffer);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   328
        encodeBuffer(buf, aStream);
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   329
    }
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
   330
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}