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