View Javadoc

1   /***
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activeio.adapter;
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  
23  import org.activeio.Packet;
24  import org.activeio.packet.AppendedPacket;
25  import org.activeio.packet.ByteArrayPacket;
26  
27  /***
28   *
29   */
30  final public class PacketByteArrayOutputStream extends OutputStream {
31      
32      private Packet result;
33      private Packet current;
34      int nextAllocationSize=0;
35      
36      public PacketByteArrayOutputStream() {
37      	this( 1024 );
38      }
39      
40      public PacketByteArrayOutputStream(int initialSize) {
41      	nextAllocationSize = initialSize;
42          current = allocate();
43      }
44      
45      protected Packet allocate() {
46      	ByteArrayPacket packet = new ByteArrayPacket(new byte[nextAllocationSize]);
47      	nextAllocationSize <<= 3; // x by 8
48          return packet;
49      }
50      
51      public void skip(int size) {
52          while( size > 0 ) {
53              if( !current.hasRemaining() ) {
54                  allocatedNext();
55              }
56              
57              int skip = ((size <= current.remaining()) ? size : current.remaining());
58              current.position(current.position()+skip);
59              size -= skip;
60          }
61      }
62      
63      public void write(int b) throws IOException {
64          if( !current.hasRemaining() ) {
65              allocatedNext();
66          }
67          current.write(b);
68      }
69      
70      public void write(byte[] b, int off, int len) throws IOException {
71          while( len > 0 ) {
72  	        if( !current.hasRemaining() ) {
73  	            allocatedNext();
74  	        }
75  	        int wrote = current.write(b,off,len);
76  	        off+=wrote;
77  	        len-=wrote;
78          }
79      }
80      
81      private void allocatedNext() {
82          if( result == null ) {
83              current.flip();
84              result = current;
85          } else {
86              current.flip();
87              result = AppendedPacket.join(result, current);            
88          }
89          current = allocate();
90      }
91      
92      public Packet getPacket() {
93          if( result == null ) {
94              current.flip();
95              return current.slice();
96          } else {
97              current.flip();
98              return AppendedPacket.join(result, current);                
99          }
100     }
101 
102     public void reset() {
103         result = null;
104         current.clear();
105     }
106 
107     public int position() {
108         return current.position() + (result==null ? 0 : result.remaining());
109     }
110 }