001: /*
002: * $Id: ASCIIHexDecode.java,v 1.2 2007/12/20 18:33:32 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.decode;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.nio.ByteBuffer;
026:
027: import com.sun.pdfview.PDFObject;
028: import com.sun.pdfview.PDFParseException;
029: import com.sun.pdfview.PDFFile;
030:
031: /**
032: * decode an array of hex nybbles into a byte array
033: *
034: * @author Mike Wessler
035: */
036: public class ASCIIHexDecode {
037: private ByteBuffer buf;
038:
039: /**
040: * initialize the decoder with an array of bytes in ASCIIHex format
041: */
042: private ASCIIHexDecode(ByteBuffer buf) {
043: this .buf = buf;
044: }
045:
046: /**
047: * get the next character from the input
048: * @return a number from 0-15, or -1 for the end character
049: */
050: private int readHexDigit() throws PDFParseException {
051: // read until we hit a non-whitespace character or the
052: // end of the stream
053: while (buf.remaining() > 0) {
054: int c = (int) buf.get();
055:
056: // see if we found a useful character
057: if (!PDFFile.isWhiteSpace((char) c)) {
058: if (c >= '0' && c <= '9') {
059: c -= '0';
060: } else if (c >= 'a' && c <= 'f') {
061: c -= 'a' - 10;
062: } else if (c >= 'A' && c <= 'F') {
063: c -= 'A' - 10;
064: } else if (c == '>') {
065: c = -1;
066: } else {
067: // unknown character
068: throw new PDFParseException("Bad character " + c
069: + "in ASCIIHex decode");
070: }
071:
072: // return the useful character
073: return c;
074: }
075: }
076:
077: // end of stream reached
078: throw new PDFParseException("Short stream in ASCIIHex decode");
079: }
080:
081: /**
082: * decode the array
083: * @return the decoded bytes
084: */
085: private ByteBuffer decode() throws PDFParseException {
086: // start at the beginning of the buffer
087: buf.rewind();
088:
089: // allocate the output buffer
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091:
092: while (true) {
093: int first = readHexDigit();
094: int second = readHexDigit();
095:
096: if (first == -1) {
097: break;
098: } else if (second == -1) {
099: baos.write((byte) (first << 4));
100: break;
101: } else {
102: baos.write((byte) ((first << 4) + second));
103: }
104: }
105:
106: return ByteBuffer.wrap(baos.toByteArray());
107: }
108:
109: /**
110: * decode an array of bytes in ASCIIHex format.
111: * <p>
112: * ASCIIHex format consists of a sequence of Hexidecimal
113: * digits, with possible whitespace, ending with the
114: * '>' character.
115: *
116: * @param buf the encoded ASCII85 characters in a byte
117: * buffer
118: * @param params parameters to the decoder (ignored)
119: * @return the decoded bytes
120: */
121: public static ByteBuffer decode(ByteBuffer buf, PDFObject params)
122: throws PDFParseException {
123: ASCIIHexDecode me = new ASCIIHexDecode(buf);
124: return me.decode();
125: }
126: }
|