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: | 210 |  | Methods: | 26
| NCLOC: | 136 |  | 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
BytePacket.java | 0% | 0% | 0% | 0% |

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 BytePacket implements Packet {
32 |  |
33 |  |
         private byte data;
34 |  |
         private byte position;
35 |  |
         private byte limit;
36 |  |
37 | 0 |
         public BytePacket(byte data) {
38 | 0 |
             this.data = data;
39 | 0 |
             clear();
40 |  |
         }
41 |  |
42 | 0 |
         public int position() {
43 | 0 |
             return position;
44 |  |
         }
45 |  |
46 | 0 |
         public void position(int position) {
47 | 0 |
             this.position = (byte) position;
48 |  |
         }
49 |  |
50 | 0 |
         public int limit() {
51 | 0 |
             return limit;
52 |  |
         }
53 |  |
54 | 0 |
         public void limit(int limit) {
55 | 0 |
             this.limit = (byte) limit;
56 |  |
         }
57 |  |
58 | 0 |
         public void flip() {
59 | 0 |
             limit(position());
60 | 0 |
             position(0);
61 |  |
         }
62 |  |
63 | 0 |
         public int remaining() {
64 | 0 |
             return limit() - position();
65 |  |
         }
66 |  |
67 | 0 |
         public void rewind() {
68 | 0 |
             position(0);
69 |  |
         }
70 |  |
71 | 0 |
         public boolean hasRemaining() {
72 | 0 |
             return remaining() > 0;
73 |  |
         }
74 |  |
75 | 0 |
         public void clear() {
76 | 0 |
             position(0);
77 | 0 |
             limit(capacity());
78 |  |
         }
79 |  |
80 | 0 |
         public int capacity() {
81 | 0 |
             return 1;
82 |  |
         }
83 |  |
84 | 0 |
         public Packet slice() {
85 | 0 |
             if( hasRemaining() )
86 | 0 |
                 return new BytePacket(data);
87 | 0 |
             return EmptyPacket.EMPTY_PACKET;
88 |  |
         }
89 |  |
90 | 0 |
         public Packet duplicate() {
91 | 0 |
             BytePacket packet = new BytePacket(data);
92 | 0 |
             packet.limit(limit());
93 | 0 |
             packet.position(position());
94 | 0 |
             return packet;
95 |  |
         }
96 |  |
97 | 0 |
         public Object duplicate(ClassLoader cl) throws IOException {
98 | 0 |
             try {
99 | 0 |
                 Class clazz = cl.loadClass(BytePacket.class.getName());
100 | 0 |
                 Constructor constructor = clazz.getConstructor(new Class[]{byte.class});
101 | 0 |
                 return constructor.newInstance(new Object[]{new Byte(data)});
102 |  |
             } catch (Throwable e) {
103 | 0 |
                 throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e);
104 |  |
             }
105 |  |
         }
106 |  |
107 | 0 |
         public void writeTo(OutputStream out) throws IOException {
108 | 0 |
             if( hasRemaining() ) {
109 | 0 |
                 out.write(data);
110 | 0 |
                 position(1);
111 |  |
             }
112 |  |
         }
113 |  |
114 | 0 |
         public void writeTo(DataOutput out) throws IOException {
115 | 0 |
             if( hasRemaining() ) {
116 | 0 |
                 out.write(data);
117 | 0 |
                 position(1);
118 |  |
             }
119 |  |
         }
120 |  |
121 |  |
         /**
122 |  |
          * @see org.activeio.Packet#read()
123 |  |
          */
124 | 0 |
         public int read() {
125 | 0 |
             if( !hasRemaining() )
126 | 0 |
                 return -1;
127 | 0 |
             position(1);
128 | 0 |
             return data & 0xff;
129 |  |
         }
130 |  |
131 |  |
         /**
132 |  |
          * @see org.activeio.Packet#read(byte[], int, int)
133 |  |
          */
134 | 0 |
         public int read(byte[] data, int offset, int length) {
135 | 0 |
             if( !hasRemaining() )
136 | 0 |
                 return -1;
137 |  |
138 | 0 |
             if( length > 0 ) {
139 | 0 |
                 data[offset] = this.data;
140 | 0 |
                 position(1);
141 | 0 |
                 return 1;
142 |  |
             }
143 | 0 |
             return 0;
144 |  |
         }
145 |  |
146 |  |
         /**
147 |  |
          * @see org.activeio.Packet#write(int)
148 |  |
          */
149 | 0 |
         public boolean write(int data) {
150 | 0 |
             if( !hasRemaining() )
151 | 0 |
                 return false;
152 |  |
153 | 0 |
             this.data = (byte) data;
154 | 0 |
             position(1);
155 | 0 |
             return true;
156 |  |
         }
157 |  |
158 |  |
         /**
159 |  |
          * @see org.activeio.Packet#write(byte[], int, int)
160 |  |
          */
161 | 0 |
         public int write(byte[] data, int offset, int length) {
162 | 0 |
             if( !hasRemaining() )
163 | 0 |
                 return -1;
164 |  |
165 | 0 |
             if( length > 0 ) {
166 | 0 |
                 this.data = data[offset] ;
167 | 0 |
                 position(1);
168 | 0 |
                 return 1;
169 |  |
             }
170 | 0 |
             return 0;
171 |  |
         }
172 |  |
173 | 0 |
         public ByteSequence asByteSequence() {
174 | 0 |
             return null;
175 |  |
         }
176 |  |
177 |  |
         /**
178 |  |
          * @see org.activeio.Packet#sliceAsBytes()
179 |  |
          */
180 | 0 |
         public byte[] sliceAsBytes() {
181 | 0 |
             return null;
182 |  |
         }
183 |  |
184 |  |
         /**
185 |  |
          * @param dest
186 |  |
          * @return the number of bytes read into the dest.
187 |  |
          */
188 | 0 |
         public int read(Packet dest) {
189 | 0 |
             if( hasRemaining() ) {
190 | 0 |
                 dest.write(data);
191 | 0 |
                 position(1);
192 | 0 |
                 return 1;
193 |  |
             }
194 | 0 |
             return 0;
195 |  |
         }
196 |  |
197 | 0 |
         public String toString() {
198 | 0 |
             return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}";
199 |  |
         }
200 |  |
201 | 0 |
         public Object narrow(Class target) {
202 | 0 |
             if( target.isAssignableFrom(getClass()) ) {
203 | 0 |
                 return this;
204 |  |
             }
205 | 0 |
             return null;
206 |  |
         }
207 |  |
208 | 0 |
         public void dispose() {
209 |  |
         }
210 |  |
     }