1 | /* |
2 | * $Id: UnsignedByteBuffer.java,v 1.2 2005/12/16 22:32:46 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | package com.moesol.nio; |
13 | |
14 | import java.nio.ByteBuffer; |
15 | import java.nio.ByteOrder; |
16 | |
17 | import com.moesol.bindings.NativeStructure; |
18 | |
19 | |
20 | /** |
21 | * Help getting bytes back as unsigned. |
22 | * |
23 | * @author Hastings |
24 | */ |
25 | public class UnsignedByteBuffer implements BufferMarker, Comparable { |
26 | |
27 | private final ByteBuffer m_byte_buffer; |
28 | |
29 | public UnsignedByteBuffer(ByteBuffer b) { |
30 | m_byte_buffer = b; |
31 | } |
32 | public static UnsignedByteBuffer allocate(int capacity) { |
33 | return new UnsignedByteBuffer(ByteBuffer.allocate(capacity)); |
34 | } |
35 | |
36 | public short get() { |
37 | short r = m_byte_buffer.get(); |
38 | return makeUnsigned(r); |
39 | } |
40 | public short get(int offset) { |
41 | return makeUnsigned(m_byte_buffer.get(offset)); |
42 | } |
43 | public void get(short[] result) { |
44 | for (int i = 0; i < result.length; i++) { |
45 | result[i] = get(); |
46 | } |
47 | } |
48 | |
49 | public void put(short v) { |
50 | m_byte_buffer.put((byte)v); |
51 | } |
52 | public void put(int offset, short v) { |
53 | m_byte_buffer.put(offset, (byte)v); |
54 | } |
55 | public void put(short[] values) { |
56 | for (int i = 0; i < values.length; i++) { |
57 | put(values[i]); |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * @param r |
63 | * @return unsigned short |
64 | */ |
65 | private short makeUnsigned(short r) { |
66 | return (short)(r & 0xFF); |
67 | } |
68 | |
69 | /** |
70 | * @return shorts remaining |
71 | */ |
72 | public int remaining() { |
73 | return m_byte_buffer.remaining(); |
74 | } |
75 | |
76 | /** |
77 | * @return number of shorts in this buffer |
78 | */ |
79 | public int limit() { |
80 | return m_byte_buffer.limit(); |
81 | } |
82 | |
83 | /** |
84 | * @return number of shorts this buffer could store |
85 | */ |
86 | public int capacity() { |
87 | return m_byte_buffer.capacity(); |
88 | } |
89 | /** |
90 | * @param bo |
91 | */ |
92 | public void order(ByteOrder bo) { |
93 | m_byte_buffer.order(bo); |
94 | } |
95 | public BufferMarker flip() { |
96 | m_byte_buffer.flip(); |
97 | return this; |
98 | } |
99 | /** |
100 | * @return underlying byte buffer |
101 | */ |
102 | public ByteBuffer getByteBuffer() { |
103 | return m_byte_buffer; |
104 | } |
105 | |
106 | public int compareTo(Object o) { |
107 | UnsignedByteBuffer other = (UnsignedByteBuffer)o; |
108 | return m_byte_buffer.compareTo(other.getByteBuffer()); |
109 | } |
110 | |
111 | private int sizeof() { |
112 | return NativeStructure.getUnsignedByteSize(); |
113 | } |
114 | public int position() { |
115 | return m_byte_buffer.position() / sizeof(); |
116 | } |
117 | public BufferMarker position(int newPosition) { |
118 | m_byte_buffer.position(newPosition * sizeof()); |
119 | return this; |
120 | } |
121 | public BufferMarker limit(int newLimit) { |
122 | m_byte_buffer.limit(newLimit * sizeof()); |
123 | return this; |
124 | } |
125 | public BufferMarker mark() { |
126 | m_byte_buffer.mark(); |
127 | return this; |
128 | } |
129 | public BufferMarker reset() { |
130 | m_byte_buffer.reset(); |
131 | return this; |
132 | } |
133 | public BufferMarker clear() { |
134 | m_byte_buffer.clear(); |
135 | return this; |
136 | } |
137 | public BufferMarker rewind() { |
138 | m_byte_buffer.rewind(); |
139 | return this; |
140 | } |
141 | public boolean hasRemaining() { |
142 | return m_byte_buffer.hasRemaining(); |
143 | } |
144 | public boolean isReadOnly() { |
145 | return m_byte_buffer.isReadOnly(); |
146 | } |
147 | |
148 | public boolean equals(Object arg0) { |
149 | return 0 == compareTo(arg0); |
150 | } |
151 | |
152 | public int hashCode() { |
153 | return m_byte_buffer.hashCode(); |
154 | } |
155 | |
156 | } |