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: | 237 |  | Methods: | 28
| NCLOC: | 160 |  | 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
ByteArrayPacket.java | 75% | 82.1% | 82.1% | 81.1% |

1 |  |
     /**
2 |  |
      *
3 |  |
      * Copyright 2004 Hiram Chirino
4 |  |
      *
5 |  |
      * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 |  |
      * use this file except in compliance with the License. You may obtain a copy of
7 |  |
      * 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, WITHOUT
13 |  |
      * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 |  |
      * License for the specific language governing permissions and limitations under
15 |  |
      * the License.
16 |  |
      */
17 |  |
     package org.activeio.packet;
18 |  |
19 |  |
     import java.io.DataOutput;
20 |  |
     import java.io.IOException;
21 |  |
     import java.io.OutputStream;
22 |  |
     import java.lang.reflect.Constructor;
23 |  |
24 |  |
     import org.activeio.Packet;
25 |  |
26 |  |
     /**
27 |  |
      * Provides a Packet implementation that is directly backed by a <code>byte[]</code>.
28 |  |
      *
29 |  |
      * @version $Revision$
30 |  |
      */
31 |  |
     final public class ByteArrayPacket implements Packet {
32 |  |
33 |  |
         private final byte buffer[];
34 |  |
35 |  |
         private final int offset;
36 |  |
         private final int capacity;
37 |  |
         private int position;
38 |  |
         private int limit;
39 |  |
         private int remaining;
40 |  |
41 |  |
42 | 12436 |
         public ByteArrayPacket(byte buffer[]) {
43 | 12436 |
             this(buffer,0, buffer.length);
44 |  |
         }
45 |  |
46 | 35300 |
         public ByteArrayPacket(byte buffer[], int offset, int capacity) {
47 | 35300 |
             this.buffer = buffer;
48 | 35300 |
             this.offset=offset;
49 | 35300 |
             this.capacity=capacity;
50 | 35300 |
             this.position = 0;
51 | 35300 |
             this.limit = capacity;
52 | 35300 |
             this.remaining = limit-position;
53 |  |
         }
54 |  |
55 | 4184 |
         public int position() {
56 | 4184 |
             return position;
57 |  |
         }
58 |  |
59 | 8460 |
         public void position(int position) {
60 | 8460 |
             this.position = position;
61 | 8460 |
             remaining = limit-position;
62 |  |
         }
63 |  |
64 | 74 |
         public int limit() {
65 | 74 |
             return limit;
66 |  |
         }
67 |  |
68 | 144 |
         public void limit(int limit) {
69 | 144 |
             this.limit = limit;
70 | 144 |
             remaining = limit-position;
71 |  |
         }
72 |  |
73 | 4248 |
         public void flip() {
74 | 4248 |
             limit = position;
75 | 4248 |
             position = 0;
76 | 4248 |
             remaining = limit - position;
77 |  |
         }
78 |  |
79 | 24578 |
         public int remaining() {
80 | 24578 |
             return remaining;
81 |  |
         }
82 |  |
83 | 6 |
         public void rewind() {
84 | 6 |
             position = 0;
85 | 6 |
             remaining = limit - position;
86 |  |
         }
87 |  |
88 | 38488 |
         public boolean hasRemaining() {
89 | 38488 |
             return remaining > 0;
90 |  |
         }
91 |  |
92 | 24386 |
         public void clear() {
93 | 24386 |
             position = 0;
94 | 24386 |
             limit = capacity;
95 | 24386 |
             remaining = limit - position;
96 |  |
         }
97 |  |
98 | 2086 |
         public int capacity() {
99 | 2086 |
             return capacity;
100 |  |
         }
101 |  |
102 | 8686 |
         public Packet slice() {
103 | 8686 |
             return new ByteArrayPacket(buffer, offset+position, remaining);
104 |  |
         }
105 |  |
106 | 12170 |
         public Packet duplicate() {
107 | 12170 |
             return new ByteArrayPacket(buffer, offset, capacity);
108 |  |
         }
109 |  |
110 | 2004 |
         public Object duplicate(ClassLoader cl) throws IOException {
111 | 2004 |
             try{
112 | 2004 |
                 Class clazz = cl.loadClass(ByteArrayPacket.class.getName());
113 | 2004 |
                 Constructor constructor = clazz.getConstructor(new Class[]{byte[].class, int.class, int.class});
114 | 2004 |
                 return constructor.newInstance(new Object[]{buffer, new Integer(offset), new Integer(capacity())});
115 |  |
             } catch (Throwable e) {
116 | 0 |
                 throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e);
117 |  |
             }
118 |  |
         }
119 |  |
120 | 4120 |
         public void writeTo(OutputStream out) throws IOException {
121 | 4120 |
             out.write(buffer, offset+position, remaining);
122 | 4120 |
             position=limit;
123 | 4120 |
             remaining = limit-position;
124 |  |
         }
125 |  |
126 | 0 |
         public void writeTo(DataOutput out) throws IOException {
127 | 0 |
             out.write(buffer, offset+position, remaining);
128 | 0 |
             position=limit;
129 | 0 |
             remaining = limit-position;
130 |  |
         }
131 |  |
132 |  |
         /**
133 |  |
          * @see org.activeio.Packet#read()
134 |  |
          */
135 | 1295 |
         public int read() {
136 | 1295 |
             if( !(remaining > 0) )
137 | 2 |
                 return -1;
138 | 1293 |
             int rc = buffer[offset+position];
139 | 1293 |
             position++;
140 | 1293 |
             remaining = limit-position;
141 | 1293 |
             return rc & 0xff;
142 |  |
         }
143 |  |
144 |  |
         /**
145 |  |
          * @see org.activeio.Packet#read(byte[], int, int)
146 |  |
          */
147 | 78 |
         public int read(byte[] data, int offset, int length) {
148 | 78 |
             if( !(remaining > 0) )
149 | 16 |
                 return -1;
150 |  |
151 | 62 |
             int copyLength = ((length <= remaining) ? length : remaining);
152 | 62 |
             System.arraycopy(buffer, this.offset+position, data, offset, copyLength);
153 | 62 |
             position += copyLength;
154 | 62 |
             remaining = limit-position;
155 | 62 |
             return copyLength;
156 |  |
         }
157 |  |
158 |  |
         /**
159 |  |
          * @see org.activeio.Packet#write(int)
160 |  |
          */
161 | 1592 |
         public boolean write(int data) {
162 | 1592 |
             if( !(remaining > 0) )
163 | 2 |
                 return false;
164 | 1590 |
             buffer[offset+position]=(byte) data;
165 | 1590 |
             position++;
166 | 1590 |
             remaining = limit-position;
167 | 1590 |
             return true;
168 |  |
         }
169 |  |
170 |  |
         /**
171 |  |
          * @see org.activeio.Packet#write(byte[], int, int)
172 |  |
          */
173 | 8246 |
         public int write(byte[] data, int offset, int length) {
174 | 8246 |
             if( !(remaining > 0) )
175 | 16 |
                 return -1;
176 |  |
177 | 8230 |
             int copyLength = ((length <= remaining) ? length : remaining);
178 | 8230 |
             System.arraycopy(data, offset, buffer, this.offset+position, copyLength);
179 | 8230 |
             position+=copyLength;
180 | 8230 |
             remaining = limit-position;
181 | 8230 |
             return copyLength;
182 |  |
         }
183 |  |
184 | 8255 |
         public ByteSequence asByteSequence() {
185 | 8255 |
             return new ByteSequence(buffer, offset+position, remaining);
186 |  |
         }
187 |  |
188 |  |
         /**
189 |  |
          * @see org.activeio.Packet#sliceAsBytes()
190 |  |
          */
191 | 24288 |
         public byte[] sliceAsBytes() {
192 | 24288 |
             if( buffer.length == remaining ) {
193 | 24288 |
                 return buffer;
194 |  |
             } else {
195 | 0 |
                 byte rc[] = new byte[remaining];
196 | 0 |
                 int op = position;
197 | 0 |
                 write(rc,0,remaining);
198 | 0 |
                 position=op;
199 | 0 |
                 remaining = limit-position;
200 | 0 |
                 return rc;
201 |  |
             }
202 |  |
         }
203 |  |
204 |  |
         /**
205 |  |
          * @param dest
206 |  |
          * @return the number of bytes read into the dest.
207 |  |
          */
208 | 12198 |
         public int read(Packet dest) {
209 | 12198 |
             int a = dest.remaining();
210 | 12198 |
             int rc = ((a <= remaining) ? a : remaining);
211 | 12198 |
             if( rc > 0 ) {
212 | 12198 |
                 dest.write( buffer, offset+position, rc);
213 | 12198 |
                 position = position+rc;
214 | 12198 |
                 remaining = limit-position;
215 |  |
             }
216 | 12198 |
             return rc;
217 |  |
         }
218 |  |
219 | 0 |
         public String toString() {
220 | 0 |
             return "{position="+position+",limit="+limit+",capacity="+capacity+"}";
221 |  |
         }
222 |  |
223 | 0 |
         public Object narrow(Class target) {
224 | 0 |
             if( target.isAssignableFrom(getClass()) ) {
225 | 0 |
                 return this;
226 |  |
             }
227 | 0 |
             return null;
228 |  |
         }
229 |  |
230 | 0 |
         public byte[] getBuffer() {
231 | 0 |
             return buffer;
232 |  |
         }
233 |  |
234 | 0 |
         public void dispose() {
235 |  |
         }
236 |  |
     }
237 |  |