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.EOFException; 020 import java.io.IOException; 021 import java.io.OutputStream; 022 023 024 025 /** 026 * Very similar to the java.io.ByteArrayOutputStream but this version 027 * is not thread safe and the resulting data is returned in a Buffer 028 * to avoid an extra byte[] allocation. It also does not re-grow it's 029 * internal buffer. 030 * 031 * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 032 */ 033 final public class BufferOutputStream extends OutputStream { 034 035 byte buffer[]; 036 int offset; 037 int limit; 038 int pos; 039 040 public BufferOutputStream(int size) { 041 this(new byte[size]); 042 } 043 044 public BufferOutputStream(byte[] buffer) { 045 this.buffer = buffer; 046 this.limit = buffer.length; 047 } 048 049 public BufferOutputStream(Buffer data) { 050 this.buffer = data.data; 051 this.pos = this.offset = data.offset; 052 this.limit = data.offset+data.length; 053 } 054 055 056 public void write(int b) throws IOException { 057 int newPos = pos + 1; 058 checkCapacity(newPos); 059 buffer[pos] = (byte) b; 060 pos = newPos; 061 } 062 063 public void write(byte b[], int off, int len) throws IOException { 064 int newPos = pos + len; 065 checkCapacity(newPos); 066 System.arraycopy(b, off, buffer, pos, len); 067 pos = newPos; 068 } 069 070 public Buffer getNextBuffer(int len) throws IOException { 071 int newPos = pos + len; 072 checkCapacity(newPos); 073 return new Buffer(buffer, pos, len); 074 } 075 076 /** 077 * Ensures the the buffer has at least the minimumCapacity specified. 078 * @param i 079 * @throws EOFException 080 */ 081 private void checkCapacity(int minimumCapacity) throws IOException { 082 if( minimumCapacity > limit ) { 083 throw new EOFException("Buffer limit reached."); 084 } 085 } 086 087 public void reset() { 088 pos = offset; 089 } 090 091 public Buffer toBuffer() { 092 return new Buffer(buffer, offset, pos); 093 } 094 095 public byte[] toByteArray() { 096 return toBuffer().toByteArray(); 097 } 098 099 public int size() { 100 return offset-pos; 101 } 102 103 104 }