001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    
033    package com.imagero.uio.io;
034    
035    import java.io.FilterInputStream;
036    import java.io.IOException;
037    import java.io.InputStream;
038    
039    /**
040     * @author Andrey Kuznetsov
041     */
042    public class HexInputStream extends FilterInputStream {
043    
044        private static final char encodeTable[] = {
045            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
046        };
047    
048        public static final int decodeTable [] = new int[0x100];
049    
050        static {
051            for (int i = 0; i < decodeTable.length; i++) {
052                decodeTable[i] = -1;
053            }
054            for (int j = 0; j < encodeTable.length; j++) {
055                decodeTable[encodeTable[j]] = j;
056            }
057        }
058    
059        boolean finished;
060    
061        byte[] buffer = new byte[80];
062        int count;
063        int pos;
064    
065        public HexInputStream(InputStream in) {
066            super(in);
067        }
068    
069        public int read() throws IOException {
070            if (pos >= count) {
071                if (finished) {
072                    return -1;
073                }
074                else {
075                    fillBuffer();
076                }
077            }
078            if (pos < count) {
079                return buffer[pos++] & 0xFF;
080            }
081            return -1;
082        }
083    
084        protected void fillBuffer() throws IOException {
085            int k = 0;
086            try {
087                for (; k < buffer.length; k++) {
088                    int b0 = in.read();
089                    if (b0 == 13 || b0 == 10) {
090                        k--;
091                        continue;
092                    }
093                    if (b0 == '>') {
094                        k++;
095                        finished = true;
096                        break;
097                    }
098                    int b1 = in.read();
099                    int d0 = decodeTable[b0];
100                    int d1 = decodeTable[b1];
101                    if (d0 == -1 || d1 == -1) {
102                        k--;
103                        continue;
104                    }
105                    buffer[k] = (byte) (d0 * 16 + d1);
106                }
107            }
108            catch (Throwable ex) {
109    //            ex.printStackTrace();
110                System.err.println(ex.getMessage());
111            }
112            count = k;
113            pos = 0;
114        }
115    
116        public long skip(long n) throws IOException {
117            long i = 0;
118            for (; i < n; i++) {
119                int a = read();
120                if (a == -1) {
121                    break;
122                }
123            }
124            return i;
125        }
126    
127        public int read(byte b[]) throws IOException {
128            return read(b, 0, b.length);
129        }
130    
131        public int read(byte b[], int off, int len) throws IOException {
132            if (b == null) {
133                return (int) skip(len);
134            }
135            if (len <= 0) {
136                return 0;
137            }
138            int i = 0;
139            try {
140                for (; i < len; i++) {
141                    int a = read();
142                    if (a == -1) {
143                        break;
144                    }
145                    b[i] = (byte) a;
146                }
147            }
148            catch (IOException ex) {
149            }
150            return i == 0 ? -1 : i;
151        }
152    }