All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mk300.marshal.minimum.io.BAOutputStream Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * Copyright 2014 Masazumi Kobayashi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mk300.marshal.minimum.io;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;

/**
 * 
 * @author [email protected]
 *
 */
public class BAOutputStream extends OutputStream {


    protected byte buf[];
    protected int count;
    
    public BAOutputStream() {
        this(32);
    }

    public BAOutputStream(int size) {
        buf = new byte[size];
    }


    public void ensureCapacity2(int requireCapacity) {
        if (buf.length - count - requireCapacity < 0)
            grow(count + requireCapacity);
    }
    
    private void ensureCapacity(int minCapacity) {
        if (minCapacity - buf.length > 0)
            grow(minCapacity);
    }

    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = buf.length;
        int newCapacity = oldCapacity << 1;
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity < 0) {
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            newCapacity = Integer.MAX_VALUE;
        }
        buf = Arrays.copyOf(buf, newCapacity);
    }
    public void write(int b) {
        ensureCapacity(count + 1);
        buf[count] = (byte) b;
        count += 1;
    }

    public void write(byte b[], int off, int len) {
        if ((off < 0) || (off > b.length) || (len < 0) ||
            ((off + len) - b.length > 0)) {
            throw new IndexOutOfBoundsException();
        }
        ensureCapacity(count + len);
        System.arraycopy(b, off, buf, count, len);
        count += len;
    }

    public void writeTo(OutputStream out) throws IOException {
        out.write(buf, 0, count);
    }

    public void reset() {
        count = 0;
    }

    public byte toByteArray()[] {
        return Arrays.copyOf(buf, count);
    }

    public int size() {
        return count;
    }

    @Override
    public void close() throws IOException {
    }
    
    public final byte[] getBuf() {
    	return buf;
    }
    
	public final void incCount(int value) {
		int temp = count + value;
		if (temp < 0) {
			temp = Integer.MAX_VALUE;
		}
		count = temp;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy