001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.fusesource.hawtbuf;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    
022    
023    /**
024     * Very similar to the java.io.ByteArrayInputStream but this version is not
025     * thread safe.
026     *
027     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
028     */
029    public class ByteArrayInputStream extends InputStream {
030    
031        byte buffer[];
032        int limit;
033        int pos;
034        int mark;
035    
036        public ByteArrayInputStream(byte data[]) {
037            this(data, 0, data.length);
038        }
039    
040        public ByteArrayInputStream(Buffer buffer) {
041            this(buffer.getData(), buffer.getOffset(), buffer.getLength());
042        }
043    
044        public ByteArrayInputStream(byte data[], int offset, int size) {
045            this.buffer = data;
046            this.mark = offset;
047            this.pos = offset;
048            this.limit = offset + size;
049        }
050    
051        public int read() throws IOException {
052            if (pos < limit) {
053                return buffer[pos++] & 0xff;
054            } else {
055                return -1;
056            }
057        }
058    
059        public int read(byte[] b) throws IOException {
060            return read(b, 0, b.length);
061        }
062    
063        public int read(byte b[], int off, int len) {
064            if (pos < limit) {
065                len = Math.min(len, limit - pos);
066                if (len > 0) {
067                    System.arraycopy(buffer, pos, b, off, len);
068                    pos += len;
069                }
070                return len;
071            } else {
072                return -1;
073            }
074        }
075    
076        public long skip(long len) throws IOException {
077            if (pos < limit) {
078                len = Math.min(len, limit - pos);
079                if (len > 0) {
080                    pos += len;
081                }
082                return len;
083            } else {
084                return -1;
085            }
086        }
087    
088        public int available() {
089            return limit - pos;
090        }
091    
092        public boolean markSupported() {
093            return true;
094        }
095    
096        public void mark(int markpos) {
097            mark = pos;
098        }
099    
100        public void reset() {
101            pos = mark;
102        }
103    }