author | lana |
Sun, 14 Jan 2018 22:25:53 -0800 | |
changeset 48536 | d7995ed9627d |
parent 47216 | 71c04702a3d5 |
child 59024 | b046ba510bbc |
permissions | -rw-r--r-- |
2 | 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 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
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 | 32 |
import java.io.PrintStream; |
33 |
import java.io.OutputStream; |
|
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 | 36 |
|
37 |
/** |
|
38 |
* This class encodes a buffer into the classic: "Hexadecimal Dump" format of |
|
39 |
* the past. It is useful for analyzing the contents of binary buffers. |
|
40 |
* The format produced is as follows: |
|
41 |
* <pre> |
|
42 |
* xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................ |
|
43 |
* </pre> |
|
44 |
* Where xxxx is the offset into the buffer in 16 byte chunks, followed |
|
45 |
* by ascii coded hexadecimal bytes followed by the ASCII representation of |
|
46 |
* the bytes or '.' if they are not valid bytes. |
|
47 |
* |
|
48 |
* @author Chuck McManis |
|
49 |
*/ |
|
50 |
||
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
25859
diff
changeset
|
51 |
public class HexDumpEncoder { |
2 | 52 |
|
53 |
private int offset; |
|
54 |
private int thisLineLength; |
|
55 |
private int currentByte; |
|
56 |
private byte thisLine[] = new byte[16]; |
|
57 |
||
58 |
static void hexDigit(PrintStream p, byte x) { |
|
59 |
char c; |
|
60 |
||
61 |
c = (char) ((x >> 4) & 0xf); |
|
62 |
if (c > 9) |
|
63 |
c = (char) ((c-10) + 'A'); |
|
64 |
else |
|
65 |
c = (char)(c + '0'); |
|
66 |
p.write(c); |
|
67 |
c = (char) (x & 0xf); |
|
68 |
if (c > 9) |
|
69 |
c = (char)((c-10) + 'A'); |
|
70 |
else |
|
71 |
c = (char)(c + '0'); |
|
72 |
p.write(c); |
|
73 |
} |
|
74 |
||
75 |
protected int bytesPerAtom() { |
|
76 |
return (1); |
|
77 |
} |
|
78 |
||
79 |
protected int bytesPerLine() { |
|
80 |
return (16); |
|
81 |
} |
|
82 |
||
83 |
protected void encodeBufferPrefix(OutputStream o) throws IOException { |
|
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 | 86 |
} |
87 |
||
88 |
protected void encodeLinePrefix(OutputStream o, int len) throws IOException { |
|
89 |
hexDigit(pStream, (byte)((offset >>> 8) & 0xff)); |
|
90 |
hexDigit(pStream, (byte)(offset & 0xff)); |
|
91 |
pStream.print(": "); |
|
92 |
currentByte = 0; |
|
93 |
thisLineLength = len; |
|
94 |
} |
|
95 |
||
96 |
protected void encodeAtom(OutputStream o, byte buf[], int off, int len) throws IOException { |
|
97 |
thisLine[currentByte] = buf[off]; |
|
98 |
hexDigit(pStream, buf[off]); |
|
99 |
pStream.print(" "); |
|
100 |
currentByte++; |
|
101 |
if (currentByte == 8) |
|
102 |
pStream.print(" "); |
|
103 |
} |
|
104 |
||
105 |
protected void encodeLineSuffix(OutputStream o) throws IOException { |
|
106 |
if (thisLineLength < 16) { |
|
107 |
for (int i = thisLineLength; i < 16; i++) { |
|
108 |
pStream.print(" "); |
|
109 |
if (i == 7) |
|
110 |
pStream.print(" "); |
|
111 |
} |
|
112 |
} |
|
113 |
pStream.print(" "); |
|
114 |
for (int i = 0; i < thisLineLength; i++) { |
|
115 |
if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) { |
|
116 |
pStream.print("."); |
|
117 |
} else { |
|
118 |
pStream.write(thisLine[i]); |
|
119 |
} |
|
120 |
} |
|
121 |
pStream.println(); |
|
122 |
offset += thisLineLength; |
|
123 |
} |
|
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 | 331 |
} |