ActiveIO - 1.0

Clover coverage report - ActiveIO - 1.0 Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT FRAMES NO FRAMES |
file stats: | LOC: | 111 |  | Methods: | 10
| NCLOC: | 75 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
PacketByteArrayOutputStream.java | 0% | 0% | 0% | 0% |

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 | 0 |
         public PacketByteArrayOutputStream() {
37 | 0 |
             this( 1024 );
38 |  |
         }
39 |  |
40 | 0 |
         public PacketByteArrayOutputStream(int initialSize) {
41 | 0 |
             nextAllocationSize = initialSize;
42 | 0 |
             current = allocate();
43 |  |
         }
44 |  |
45 | 0 |
         protected Packet allocate() {
46 | 0 |
             ByteArrayPacket packet = new ByteArrayPacket(new byte[nextAllocationSize]);
47 | 0 |
             nextAllocationSize <<= 3; // x by 8
48 | 0 |
             return packet;
49 |  |
         }
50 |  |
51 | 0 |
         public void skip(int size) {
52 | 0 |
             while( size > 0 ) {
53 | 0 |
                 if( !current.hasRemaining() ) {
54 | 0 |
                     allocatedNext();
55 |  |
                 }
56 |  |
57 | 0 |
                 int skip = ((size <= current.remaining()) ? size : current.remaining());
58 | 0 |
                 current.position(current.position()+skip);
59 | 0 |
                 size -= skip;
60 |  |
             }
61 |  |
         }
62 |  |
63 | 0 |
         public void write(int b) throws IOException {
64 | 0 |
             if( !current.hasRemaining() ) {
65 | 0 |
                 allocatedNext();
66 |  |
             }
67 | 0 |
             current.write(b);
68 |  |
         }
69 |  |
70 | 0 |
         public void write(byte[] b, int off, int len) throws IOException {
71 | 0 |
             while( len > 0 ) {
72 | 0 |
                 if( !current.hasRemaining() ) {
73 | 0 |
                     allocatedNext();
74 |  |
                 }
75 | 0 |
                 int wrote = current.write(b,off,len);
76 | 0 |
                 off+=wrote;
77 | 0 |
                 len-=wrote;
78 |  |
             }
79 |  |
         }
80 |  |
81 | 0 |
         private void allocatedNext() {
82 | 0 |
             if( result == null ) {
83 | 0 |
                 current.flip();
84 | 0 |
                 result = current;
85 |  |
             } else {
86 | 0 |
                 current.flip();
87 | 0 |
                 result = AppendedPacket.join(result, current);
88 |  |
             }
89 | 0 |
             current = allocate();
90 |  |
         }
91 |  |
92 | 0 |
         public Packet getPacket() {
93 | 0 |
             if( result == null ) {
94 | 0 |
                 current.flip();
95 | 0 |
                 return current.slice();
96 |  |
             } else {
97 | 0 |
                 current.flip();
98 | 0 |
                 return AppendedPacket.join(result, current);
99 |  |
             }
100 |  |
         }
101 |  |
102 | 0 |
         public void reset() {
103 | 0 |
             result = null;
104 | 0 |
             current.clear();
105 |  |
         }
106 |  |
107 | 0 |
         public int position() {
108 | 0 |
             return current.position() + (result==null ? 0 : result.remaining());
109 |  |
         }
110 |  |
     }
111 |  |