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: | 281 |  | Methods: | 32
| NCLOC: | 172 |  | 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
ByteBufferPacket.java | 40% | 51.8% | 65.6% | 52.4% |

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 |  |
     import java.nio.ByteBuffer;
24 |  |
25 |  |
     import org.activeio.Packet;
26 |  |
27 |  |
     /**
28 |  |
      * Provides a Packet implementation that is backed by a {@see java.nio.ByteBuffer}
29 |  |
      *
30 |  |
      * @version $Revision$
31 |  |
      */
32 |  |
     final public class ByteBufferPacket implements Packet {
33 |  |
34 |  |
         public static final int DEFAULT_BUFFER_SIZE = Integer.parseInt(System.getProperty("org.activeio.DefaultByteBufferSize", ""+(64*1024)));
35 |  |
         public static final int DEFAULT_DIRECT_BUFFER_SIZE = Integer.parseInt(System.getProperty("org.activeio.DefaultDirectByteBufferSize", ""+(8*1024)));
36 |  |
37 |  |
         private final ByteBuffer buffer;
38 |  |
         private static final int TEMP_BUFFER_SIZE = 64*1024;
39 |  |
40 | 4165 |
         public ByteBufferPacket(ByteBuffer buffer) {
41 | 4165 |
             this.buffer = buffer;
42 | 4165 |
             clear();
43 |  |
         }
44 |  |
45 | 4154 |
         public ByteBuffer getByteBuffer() {
46 | 4154 |
             return buffer;
47 |  |
         }
48 |  |
49 | 44 |
         public static ByteBufferPacket createDefaultBuffer(boolean direct) {
50 | 44 |
             if( direct )
51 | 44 |
                 return new ByteBufferPacket( ByteBuffer.allocateDirect(DEFAULT_DIRECT_BUFFER_SIZE) );
52 | 0 |
             return new ByteBufferPacket( ByteBuffer.allocate(DEFAULT_BUFFER_SIZE)  );
53 |  |
         }
54 |  |
55 | 0 |
         public void writeTo(OutputStream out) throws IOException {
56 | 0 |
             if( buffer.hasArray() ) {
57 |  |
58 |  |
                 // If the buffer is backed by an array.. then use it directly.
59 | 0 |
                 out.write(buffer.array(), position(), remaining());
60 | 0 |
                 position(limit());
61 |  |
62 |  |
             } else {
63 |  |
64 |  |
                 // It's not backed by a buffer.. We can only dump it to a OutputStream via a byte[] so,
65 |  |
                 // create a temp buffer that we can use to chunk it out.
66 | 0 |
                 byte temp[] = new byte[TEMP_BUFFER_SIZE];
67 | 0 |
                 while( buffer.hasRemaining() ) {
68 | 0 |
                     int maxWrite = buffer.remaining() > temp.length ? temp.length : buffer.remaining();
69 | 0 |
                     buffer.get(temp, 0, maxWrite);
70 | 0 |
                     out.write(temp,0, maxWrite);
71 |  |
                 }
72 |  |
73 |  |
             }
74 |  |
         }
75 |  |
76 | 0 |
         public void writeTo(DataOutput out) throws IOException {
77 | 0 |
             if( buffer.hasArray() ) {
78 |  |
79 |  |
                 // If the buffer is backed by an array.. then use it directly.
80 | 0 |
                 out.write(buffer.array(), position(), remaining());
81 | 0 |
                 position(limit());
82 |  |
83 |  |
             } else {
84 |  |
85 |  |
                 // It's not backed by a buffer.. We can only dump it to a OutputStream via a byte[] so,
86 |  |
                 // create a temp buffer that we can use to chunk it out.
87 | 0 |
                 byte temp[] = new byte[TEMP_BUFFER_SIZE];
88 | 0 |
                 while( buffer.hasRemaining() ) {
89 | 0 |
                     int maxWrite = buffer.remaining() > temp.length ? temp.length : buffer.remaining();
90 | 0 |
                     buffer.get(temp, 0, maxWrite);
91 | 0 |
                     out.write(temp,0, maxWrite);
92 |  |
                 }
93 |  |
94 |  |
             }
95 |  |
         }
96 |  |
97 | 50 |
         public int capacity() {
98 | 50 |
             return buffer.capacity();
99 |  |
         }
100 |  |
101 | 8341 |
         public void clear() {
102 | 8341 |
             buffer.clear();
103 |  |
         }
104 |  |
105 | 0 |
         public Packet compact() {
106 | 0 |
             buffer.compact();
107 | 0 |
             return this;
108 |  |
         }
109 |  |
110 | 4088 |
         public void flip() {
111 | 4088 |
             buffer.flip();
112 |  |
         }
113 |  |
114 | 12306 |
         public boolean hasRemaining() {
115 | 12306 |
             return buffer.hasRemaining();
116 |  |
         }
117 |  |
118 | 0 |
         public boolean isDirect() {
119 | 0 |
             return buffer.isDirect();
120 |  |
         }
121 |  |
122 | 0 |
         public boolean isReadOnly() {
123 | 0 |
             return buffer.isReadOnly();
124 |  |
         }
125 |  |
126 | 16 |
         public int limit() {
127 | 16 |
             return buffer.limit();
128 |  |
         }
129 |  |
130 | 26 |
         public void limit(int arg0) {
131 | 26 |
             buffer.limit(arg0);
132 |  |
         }
133 |  |
134 | 0 |
         public Packet mark() {
135 | 0 |
             buffer.mark();
136 | 0 |
             return this;
137 |  |
         }
138 |  |
139 | 16 |
         public int position() {
140 | 16 |
             return buffer.position();
141 |  |
         }
142 |  |
143 | 28 |
         public void position(int arg0) {
144 | 28 |
             buffer.position(arg0);
145 |  |
         }
146 |  |
147 | 16492 |
         public int remaining() {
148 | 16492 |
             return buffer.remaining();
149 |  |
         }
150 |  |
151 | 2 |
         public void rewind() {
152 | 2 |
             buffer.rewind();
153 |  |
         }
154 |  |
155 | 2 |
         public Packet slice() {
156 | 2 |
             return new ByteBufferPacket(buffer.slice());
157 |  |
         }
158 |  |
159 | 2 |
         public Packet duplicate() {
160 | 2 |
             return new ByteBufferPacket(buffer.duplicate());
161 |  |
         }
162 |  |
163 | 0 |
         public Object duplicate(ClassLoader cl) throws IOException {
164 | 0 |
             try {
165 | 0 |
                 Class clazz = cl.loadClass(ByteBufferPacket.class.getName());
166 | 0 |
                 Constructor constructor = clazz.getConstructor(new Class[]{ByteBuffer.class});
167 | 0 |
                 return constructor.newInstance(new Object[]{buffer.duplicate()});
168 |  |
             } catch (Throwable e) {
169 | 0 |
                 throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e);
170 |  |
             }
171 |  |
172 |  |
         }
173 |  |
174 |  |
175 |  |
         /**
176 |  |
          * @see org.activeio.Packet#read()
177 |  |
          */
178 | 514 |
         public int read() {
179 | 514 |
             if( !buffer.hasRemaining() )
180 | 2 |
                 return -1;
181 | 512 |
             return buffer.get() & 0xff;
182 |  |
         }
183 |  |
184 |  |
         /**
185 |  |
          * @see org.activeio.Packet#read(byte[], int, int)
186 |  |
          */
187 | 4088 |
         public int read(byte[] data, int offset, int length) {
188 | 4088 |
             if( !hasRemaining() )
189 | 2 |
                 return -1;
190 |  |
191 | 4086 |
             int copyLength = Math.min(length, remaining());
192 | 4086 |
             buffer.get(data, offset, copyLength);
193 | 4086 |
             return copyLength;
194 |  |
         }
195 |  |
196 |  |
         /**
197 |  |
          * @see org.activeio.Packet#write(int)
198 |  |
          */
199 | 2170 |
         public boolean write(int data) {
200 | 2170 |
             if( !buffer.hasRemaining() )
201 | 2 |
                 return false;
202 | 2168 |
             buffer.put((byte)data);
203 | 2168 |
             return true;
204 |  |
         }
205 |  |
206 |  |
         /**
207 |  |
          * @see org.activeio.Packet#write(byte[], int, int)
208 |  |
          */
209 | 4150 |
         public int write(byte[] data, int offset, int length) {
210 | 4150 |
             if( !hasRemaining() )
211 | 2 |
                 return -1;
212 |  |
213 | 4148 |
             int copyLength = Math.min(length, remaining());
214 | 4148 |
             buffer.put(data, offset, copyLength);
215 | 4148 |
             return copyLength;
216 |  |
         }
217 |  |
218 |  |
         /**
219 |  |
          * @see org.activeio.Packet#asByteSequence()
220 |  |
          */
221 | 0 |
         public ByteSequence asByteSequence() {
222 | 0 |
             if( buffer.hasArray() ) {
223 | 0 |
                 byte[] bs = buffer.array();
224 | 0 |
                 return new ByteSequence(bs, buffer.position(), buffer.remaining());
225 |  |
             }
226 |  |
             // TODO: implement the direct case.
227 | 0 |
             return null;
228 |  |
         }
229 |  |
230 |  |
         /**
231 |  |
          * @see org.activeio.Packet#sliceAsBytes()
232 |  |
          */
233 | 0 |
         public byte[] sliceAsBytes() {
234 |  |
             // TODO Auto-generated method stub
235 | 0 |
             return null;
236 |  |
         }
237 |  |
238 |  |
         /**
239 |  |
          * @param dest
240 |  |
          * @return the number of bytes read into the dest.
241 |  |
          */
242 | 4066 |
         public int read(Packet dest) {
243 |  |
244 | 4066 |
             int rc = Math.min(dest.remaining(), remaining());
245 | 4066 |
             if( rc > 0 ) {
246 |  |
247 | 4066 |
                 if( dest.getClass() == ByteBufferPacket.class ) {
248 |  |
249 |  |
                     // Adjust our limit so that we don't overflow the dest buffer.
250 | 0 |
                     int limit = limit();
251 | 0 |
                     limit(position()+rc);
252 |  |
253 | 0 |
                     ((ByteBufferPacket)dest).buffer.put(buffer);
254 |  |
255 |  |
                     // restore the limit.
256 | 0 |
                     limit(limit);
257 |  |
258 | 0 |
                     return 0;
259 |  |
                 } else {
260 | 4066 |
                     ByteSequence sequence = dest.asByteSequence();
261 | 4066 |
                     rc = read(sequence.getData(), sequence.getOffset(), sequence.getLength());
262 | 4066 |
                     dest.position(dest.position()+rc);
263 |  |
                 }
264 |  |
             }
265 | 4066 |
             return rc;
266 |  |
         }
267 |  |
268 | 0 |
         public String toString() {
269 | 0 |
             return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}";
270 |  |
         }
271 |  |
272 | 20 |
         public Object narrow(Class target) {
273 | 20 |
             if( target.isAssignableFrom(getClass()) ) {
274 | 20 |
                 return this;
275 |  |
             }
276 | 0 |
             return null;
277 |  |
         }
278 |  |
279 | 0 |
         public void dispose() {
280 |  |
         }
281 |  |
     }