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    final public class BufferInputStream extends InputStream {
030    
031        byte buffer[];
032        int limit;
033        int pos;
034        int mark;
035    
036        public BufferInputStream(byte data[]) {
037            this(data, 0, data.length);
038        }
039    
040        public BufferInputStream(Buffer sequence) {
041            this(sequence.getData(), sequence.getOffset(), sequence.getLength());
042        }
043    
044        public BufferInputStream(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                System.arraycopy(buffer, pos, b, off, len);
067                pos += len;
068                return len;
069            } else {
070                return -1;
071            }
072        }
073        
074        public Buffer readBuffer(int len) {
075            Buffer rc=null;
076            if (pos < limit) {
077                len = Math.min(len, limit - pos);
078                rc = new Buffer(buffer, pos, len);
079                pos += len;
080            }
081            return rc;
082        }
083    
084        public long skip(long len) throws IOException {
085            if (pos < limit) {
086                len = Math.min(len, limit - pos);
087                if (len > 0) {
088                    pos += len;
089                }
090                return len;
091            } else {
092                return -1;
093            }
094        }
095    
096        public int available() {
097            return limit - pos;
098        }
099    
100        public boolean markSupported() {
101            return true;
102        }
103    
104        public void mark(int markpos) {
105            mark = pos;
106        }
107    
108        public void reset() {
109            pos = mark;
110        }
111    
112    }