1 | /* |
2 | * $Id: UnsignedShortBuffer.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 short (C size 4 bytes). |
22 | * Same as CharBuffer, but System.out.println does the right thing. |
23 | * |
24 | * @author Hastings |
25 | */ |
26 | public class UnsignedShortBuffer implements BufferMarker, Comparable { |
27 | |
28 | private final ByteBuffer m_byte_buffer; |
29 | |
30 | public UnsignedShortBuffer(ByteBuffer b) { |
31 | m_byte_buffer = b; |
32 | } |
33 | |
34 | /** |
35 | * @param length |
36 | * @return new byte buffre |
37 | */ |
38 | public static UnsignedShortBuffer allocate(int length) { |
39 | return new UnsignedShortBuffer(ByteBuffer.allocate(computeNumBytes(length))); |
40 | } |
41 | |
42 | public int get() { |
43 | int r = m_byte_buffer.getChar(); |
44 | return makeUnsigned(r); |
45 | } |
46 | public int get(int offset) { |
47 | return makeUnsigned(m_byte_buffer.getChar(offset)); |
48 | } |
49 | public void put(int v) { |
50 | m_byte_buffer.putChar((char)v); |
51 | } |
52 | public void put(int offset, int v) { |
53 | m_byte_buffer.putChar(offset, (char)v); |
54 | } |
55 | |
56 | /** |
57 | * @param result |
58 | */ |
59 | public void get(int[] result) { |
60 | for (int i = 0; i < result.length; i++) { |
61 | result[i] = get(); |
62 | } |
63 | } |
64 | |
65 | /** |
66 | * @param values |
67 | */ |
68 | public void put(int[] values) { |
69 | for (int i = 0; i < values.length; i++) { |
70 | put(values[i]); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * @param r |
76 | * @return unsigned short |
77 | */ |
78 | private int makeUnsigned(int r) { |
79 | return r & 0xFFFF; |
80 | } |
81 | |
82 | /** |
83 | * @return true if remaining |
84 | */ |
85 | public boolean hasRemaining() { |
86 | return m_byte_buffer.hasRemaining(); |
87 | } |
88 | |
89 | /** |
90 | * @return remaining |
91 | */ |
92 | public int remaining() { |
93 | return computeNumEls(m_byte_buffer.remaining()); |
94 | } |
95 | |
96 | /** |
97 | * @return limit |
98 | */ |
99 | public int limit() { |
100 | return computeNumEls(m_byte_buffer.limit()); |
101 | } |
102 | |
103 | /** |
104 | * @return capacity |
105 | */ |
106 | public int capacity() { |
107 | return computeNumEls(m_byte_buffer.capacity()); |
108 | } |
109 | |
110 | /** |
111 | * @return number of elements that fit n bytes. |
112 | */ |
113 | private static int computeNumEls(int bytes) { |
114 | assert(bytes % sizeof() == 0); |
115 | return bytes / sizeof(); |
116 | } |
117 | private static int computeNumBytes(int els) { |
118 | return els * sizeof(); |
119 | } |
120 | |
121 | /** |
122 | * @return underlying byte buffer |
123 | */ |
124 | public ByteBuffer getByteBuffer() { |
125 | return m_byte_buffer; |
126 | } |
127 | |
128 | public UnsignedShortBuffer order(ByteOrder o) { |
129 | m_byte_buffer.order(o); |
130 | return this; |
131 | } |
132 | |
133 | public BufferMarker flip() { |
134 | m_byte_buffer.flip(); |
135 | return this; |
136 | } |
137 | |
138 | /* (non-Javadoc) |
139 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
140 | */ |
141 | public int compareTo(Object o) { |
142 | UnsignedShortBuffer other = (UnsignedShortBuffer)o; |
143 | return m_byte_buffer.compareTo(other.getByteBuffer()); |
144 | } |
145 | |
146 | private static int sizeof() { |
147 | return NativeStructure.getUnsignedShortSize(); |
148 | } |
149 | public int position() { |
150 | return computeNumEls(m_byte_buffer.position()); |
151 | } |
152 | public BufferMarker position(int newPosition) { |
153 | m_byte_buffer.position(computeNumBytes(newPosition)); |
154 | return this; |
155 | } |
156 | public BufferMarker limit(int newLimit) { |
157 | m_byte_buffer.limit(computeNumBytes(newLimit)); |
158 | return this; |
159 | } |
160 | public BufferMarker mark() { |
161 | m_byte_buffer.mark(); |
162 | return this; |
163 | } |
164 | public BufferMarker reset() { |
165 | m_byte_buffer.reset(); |
166 | return this; |
167 | } |
168 | public BufferMarker clear() { |
169 | m_byte_buffer.clear(); |
170 | return this; |
171 | } |
172 | public BufferMarker rewind() { |
173 | m_byte_buffer.rewind(); |
174 | return this; |
175 | } |
176 | public boolean isReadOnly() { |
177 | return m_byte_buffer.isReadOnly(); |
178 | } |
179 | |
180 | public boolean equals(Object arg0) { |
181 | return 0 == compareTo(arg0); |
182 | } |
183 | |
184 | public int hashCode() { |
185 | return m_byte_buffer.hashCode(); |
186 | } |
187 | |
188 | } |