001/*
002 *  Copyright 2013 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.lang.io.buffered;
017
018import java.io.IOException;
019import java.io.InputStream;
020
021/**
022 * A BufferedInputStream where the buffer is provided by a byte array. Use this class as an alternative to {@link java.io.BufferedInputStream}
023 */
024public class ByteArrayBufferedInputStream extends AbstractBufferedInputStream {
025
026    private byte[] buf;
027
028    /**
029     * Create a new instance with the default buffer size
030     * @param in InputStream to be wrapped
031     * @see java.io.BufferedInputStream#BufferedInputStream(InputStream)
032     */
033    public ByteArrayBufferedInputStream(InputStream in) {
034        this(in, DEFAULT_BUFFER_SIZE);
035    }
036
037    /**
038     * Creates a new instance with the given buffer size in bytes.
039     * @param in InputStream to be wrapped
040     * @param size The size of the buffer in bytes
041     * @see java.io.BufferedInputStream#BufferedInputStream(InputStream, int)
042     */
043    public ByteArrayBufferedInputStream(InputStream in, int size) {
044        super(in, size);
045        buf = new byte[size];
046    }
047
048    @Override
049    protected void assertBufferOpen() throws IOException {
050        if (buf == null) {
051            throw new IOException("Stream closed");
052        }
053    }
054
055    @Override
056    protected void compact(int markpos, int size) {
057        System.arraycopy(buf, markpos, buf, 0, size);
058    }
059
060    @Override
061    protected byte[] toArray() {
062        return buf;
063    }
064
065    @Override
066    protected void resizeBuffer(int position, int newSize) throws IOException {
067
068        assertBufferOpen();
069
070        if (newSize > getMarkLimit()) {
071            newSize = getMarkLimit();
072        }
073
074        byte nbuf[] = new byte[newSize];
075        System.arraycopy(buf, 0, nbuf, 0, position);
076
077        if (buf == null) {
078            throw new IOException("Stream closed");
079        }
080
081        buf = nbuf;
082    }
083
084    @Override
085    protected int limit() {
086        return buf.length;
087    }
088
089    @Override
090    protected int getInt(int position) throws IOException {
091        assertBufferOpen();
092        return buf[position];
093    }
094
095    @Override
096    protected void get(byte[] b, int pos, int cnt) {
097        System.arraycopy(buf, pos, b, 0, cnt);
098    }
099
100    @Override
101    protected void clean() {
102        if (buf == null) {
103            return;
104        }
105
106        buf = null;
107    }
108}