-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathByteBank.java
More file actions
209 lines (191 loc) · 6.29 KB
/
ByteBank.java
File metadata and controls
209 lines (191 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2023 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.io;
/**
* A {@link ByteBank} is a self-growing buffer over arbitrary bytes.
*
* @author Gabriel Einsdorf
* @author Curtis Rueden
*/
public interface ByteBank {
/**
* @param pos the position to read from
* @return the byte at the given position
*/
public byte getByte(long pos);
/**
* @param startPos the position in the buffer to start reading from
* @param bytes the byte array to read into
* @return the number of bytes read
*/
default int getBytes(long startPos, byte[] bytes) {
return getBytes(startPos, bytes, 0, bytes.length);
}
/**
* @param startPos the position in the buffer to start reading from
* @param bytes the byte array to read into
* @param offset the offset in the bytes array
* @param length the number of elements to read into the bytes array
* @return number of bytes read
*/
int getBytes(long startPos, byte[] bytes, int offset, int length);
/**
* Copies part of this buffer into a newly allocated byte array.
*
* @param offset the initial position in the buffer
* @param len the number of bytes to copy
* @return The newly allocated byte array containing the data.
*/
default byte[] toByteArray(final long offset, final int len) {
if (offset < 0 || len < 0 || offset + len > size()) {
throw new IllegalArgumentException("Invalid range");
}
final byte[] bytes = new byte[len];
getBytes(offset, bytes);
return bytes;
}
/**
* Copies this entire buffer into a newly allocated byte array.
*
* @return The newly allocated byte array containing the data.
*/
default byte[] toByteArray() {
long max = size();
if (max > Integer.MAX_VALUE) {
throw new IllegalStateException(
"Byte bank is too large to store into a single byte[]");
}
return toByteArray(0, (int) max);
}
/**
* Sets the bytes starting form the given position to the values form the
* provided array.
*
* @param startPos the position in the buffer to start writing from
* @param bytes the byte array to write
* @param offset the offset in the bytes array
* @param length the number of bytes to read
*/
void setBytes(long startPos, byte[] bytes, int offset, int length);
/**
* Appends the given bytes to the buffer
*
* @param bytes the array containing the bytes to append to the buffer
* @param length the number of elements to append from the bytes array
*/
default void appendBytes(byte[] bytes, int length) {
appendBytes(bytes, 0, length);
}
/**
* Appends the given bytes to the buffer
*
* @param bytes the array containing the bytes to append to the buffer
* @param offset the offset in the bytes array
* @param length the number of elements to append from the bytes array
*/
default void appendBytes(byte[] bytes, int offset, int length) {
setBytes(size(), bytes, offset, length);
}
/**
* Check if we can read from the specified range
*
* @param start the start position of the range
* @param end the end position of the range
*/
default void checkReadPos(final long start, final long end) {
basicRangeCheck(start, end);
if (start > size()) {
throw new IndexOutOfBoundsException("Requested position: " + start +
" is outside the buffer: " + size());
}
}
/**
* Check if we can write to the specified range
*
* @param start the start position of the range
* @param end the end position of the range
* @throws IndexOutOfBoundsException if
*/
default void checkWritePos(final long start, final long end) {
if (start > size() + 1) { // we can't have holes in the buffer
throw new IndexOutOfBoundsException("Requested start position: " + start +
" would leave a hole in the buffer, largest legal position is: " +
size());
}
if (end < start) {
throw new IllegalArgumentException(
"Invalid range, end is smaller than start!");
}
if (end > getMaxBufferSize()) {
throw new IndexOutOfBoundsException("Requested position " + end +
" is larger than the maximal buffer size: " + getMaxBufferSize());
}
}
/**
* Ensures that the requested range satisfies basic sanity criteria.
*
* @param start the start of the range
* @param end the end of the range
*/
default void basicRangeCheck(final long start, final long end) {
if (start > size()) {
throw new IndexOutOfBoundsException("Requested position: " + start +
" is outside the buffer: " + size());
}
if (end < start) {
throw new IllegalArgumentException(
"Invalid range, end is smaller than start!");
}
}
/**
* Clears the buffer
*/
void clear();
/**
* @return the offset which follows the last byte stored in this ByteBank
*/
long size();
/**
* Sets the byte at the given position
*
* @param pos the position
* @param b the value to set
*/
void setByte(long pos, byte b);
/**
* @return the maximal size of the buffer
*/
long getMaxBufferSize();
/**
* @return True iff the buffer is read-only.
*/
default boolean isReadOnly() {
return false;
}
}