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