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