-
Notifications
You must be signed in to change notification settings - Fork 232
/
ByteFifoBuffer.java
executable file
·385 lines (359 loc) · 10.5 KB
/
ByteFifoBuffer.java
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the Jitsi community (https://jitsi.org).
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.ice4j.pseudotcp.util;
import java.nio.*;
/**
* First in - first out byte buffer
*
* @author Pawel Domas
*/
public class ByteFifoBuffer
{
/**
* Backing byte array
*/
private byte[] array;
/**
* Current write position
*/
private int write_pos = 0;
/**
* Stored bytes count
*/
private int buffered = 0;
/**
* Current read position
*/
private int read_pos = 0;
/**
* Creates buffer of specified size
*
* @param len buffer's size
*/
public ByteFifoBuffer(int len)
{
array = new byte[len];
}
/**
* @return buffer's capacity
*/
public int length()
{
return array.length;
}
/**
* Method reads <tt>count</tt> bytes into <tt>out_buffer</tt>.
* Current read position is incremented by count of bytes
* that has been successfully read.
*
* @param out_buffer
* @param count
* @return bytes successfully read
*/
public int read(byte[] out_buffer, int count)
{
return read(out_buffer, 0, count);
}
/**
* Read with buffer offset
* @param out_buffer
* @param buff_offset
* @param count bytes to read
* @return read byte count
*/
public int read(byte[] out_buffer, int buff_offset, int count) {
count = readLimit(count);
if (count > 0)
{
readOp(out_buffer, buff_offset, count, array, read_pos, array.length);
read_pos = (read_pos + count) % array.length;
buffered -= count;
}
return count;
}
/**
* Limits <tt>desiredReadCount</tt> to count that is actually available
* @param desiredReadCount
* @return
*/
private int readLimit(int desiredReadCount)
{
return desiredReadCount > buffered ? buffered : desiredReadCount;
}
/**
* Utility method used for read operations
* @param outBuffer
* @param dst_buff_offset
* @param count
* @param srcBuffer
* @param read_pos
* @param buff_len
*/
private static void readOp(byte[] outBuffer, int dst_buff_offset, int count,
byte[] srcBuffer, int read_pos, int buff_len)
{
if (read_pos + count <= buff_len)
{
//single operation
System.arraycopy(srcBuffer, read_pos, outBuffer, dst_buff_offset,
count);
}
else
{
//two operations
int tillEndCount = buff_len - read_pos;
System.arraycopy(srcBuffer, read_pos, outBuffer,
dst_buff_offset, tillEndCount);
int fromStartCount = count - tillEndCount;
System.arraycopy(srcBuffer, 0, outBuffer,
dst_buff_offset + tillEndCount, fromStartCount);
}
}
/**
*
* @return space left in buffer for write
*/
public int getWriteRemaining()
{
return array.length - buffered;
}
/**
*
* @return bytes stored in buffer and available for reading
*/
public int getBuffered()
{
return buffered;
}
/**
* Writes <tt>count</tt> of bytes from the <tt>buffer</tt>
*
* @param buffer
* @param count
* @return bytes successfully written to buffer
*/
public int write(byte[] buffer, int count)
{
return write(buffer, 0, count);
}
/**
*
* @param data source data
* @param offset source buffer's offset
* @param count
* @return byte count actually read
*/
public int write(byte[] data, int offset, int count)
{
/*
* System.out.println("----write " + this + " " + len + " buffered " +
* GetBuffered() + " buff avail: " + GetWriteRemaining());
*/
count = writeLimit(count);
writeOp(data, offset, count, array, write_pos, array.length);
write_pos = (write_pos + count) % array.length;
buffered += count;
/*
* System.out.println("----write "+this+" "+len+" buffered
* "+GetBuffered()); for(int i=0; i < len; i++){
* System.out.println("WDATA: "+data[i]); }
*/
return count;
}
/**
* Utility method for write operations
* @param inBuffer
* @param inOffset
* @param count
* @param outBuffer
* @param write_pos
* @param buff_len
*/
private static void writeOp(byte[] inBuffer,
int inOffset,
int count,
byte[] outBuffer,
int write_pos,
int buff_len)
{
if ((write_pos + count) <= buff_len)
{
//single op
System.arraycopy(inBuffer, inOffset, outBuffer, write_pos, count);
}
else
{
//till end and from beginning
int tillEndCount;
int fromStartCount;
tillEndCount = buff_len - write_pos;
fromStartCount = count - tillEndCount;
System.arraycopy(inBuffer, inOffset, outBuffer,
write_pos, tillEndCount);
System.arraycopy(inBuffer, inOffset + tillEndCount,
outBuffer, 0, fromStartCount);
}
}
/**
* Limits <tt>desiredWriteCount</tt> to what's actually available
* @param desiredWriteCount
* @return
*/
private int writeLimit(int desiredWriteCount)
{
return desiredWriteCount > (array.length - buffered) ?
(array.length - buffered) : desiredWriteCount;
}
/**
* Checks if new write position is correct
*
* @param newWrPos new write position
*/
private void assertWriteLimit(int newWrPos)
throws IllegalArgumentException
{
int spaceReq;
int availSpace = getWriteRemaining();
if (newWrPos < write_pos)
{
spaceReq = newWrPos + (array.length - write_pos);
}
else
{
spaceReq = newWrPos - write_pos;
}
if (spaceReq > availSpace)
{
throw new IllegalArgumentException();
}
}
/**
* Advances current buffer's write position by <tt>count</tt> bytes
*
* @param count
*/
public void consumeWriteBuffer(int count)
throws IllegalArgumentException,
BufferOverflowException
{
if (count > getWriteRemaining())
{
throw new BufferOverflowException();
}
if (count < 0)
{
throw new IllegalArgumentException();
}
int newPos = (write_pos + count) % array.length;
assertWriteLimit(newPos);
write_pos = newPos;
buffered += count;
}
/**
* Sets new buffer's capacity
*
* @param new_size
* @return <tt>true</tt> if operation is possible to perform, that is if new
* buffered data fits into new buffer
*/
public boolean setCapacity(int new_size)
{
if (new_size < getBuffered())
{
return false;
}
byte[] newBuff = new byte[new_size];
readOp(newBuff, 0, buffered, array, read_pos, array.length);
this.array = newBuff;
return true;
}
/**
* Aligns current read position by <tt>count</tt>
*
* @param count
* @throws BufferUnderflowException if new position exceeds buffered data
* count
*/
public void consumeReadData(int count)
throws IllegalArgumentException,
BufferUnderflowException
{
/*
* System.out.println("Consume read " + this + " " + count + " read pos:
* " + read_pos);
*/
if (count > buffered)
{
throw new BufferUnderflowException();
}
if (count < 0)
{
throw new IllegalArgumentException();
}
this.read_pos = (read_pos + count) % array.length;
buffered -= count;
}
/**
* Reads <tt>count</tt> bytes from buffer without storing new read position
*
* @param dst_buff
* @param dst_buff_offset offset of destination buffer
* @param count bytes to read
* @param offset from current read position
* @return bytes successfully read
*/
public int readOffset(byte[] dst_buff,
int dst_buff_offset,
int count,
int offset)
{
//TODO: not sure if should decrease read count or throw an exception
/*
* System.out.println("Read dst offset " + dst_buff_offset + " offset "
* + offset + " len " + count + " " + this);
*/
int read_offset = (this.read_pos + offset) % array.length;
readOp(dst_buff, dst_buff_offset, count, array, read_offset, array.length);
return count;
}
/**
* Writes <tt>count</tt> bytes from <tt>data</tt> to the buffer without
* affecting buffer's write position
*
* @param data
* @param count
* @param nOffset from buffer's write position
* @return bytes successfully written
*/
public int writeOffset(byte[] data, int count, int nOffset)
throws BufferOverflowException
{
if (count > getWriteRemaining())
{
throw new BufferOverflowException();
}
if (count < 0)
{
throw new IllegalArgumentException();
}
int offWritePos = (this.write_pos + nOffset) % array.length;
count = writeLimit(count);
assertWriteLimit(offWritePos + count);
writeOp(data, 0, count, array, offWritePos, array.length);
return count;
}
public void resetReadPosition()
{
this.read_pos = 0;
}
public void resetWritePosition()
{
this.write_pos = 0;
this.buffered = 0;
}
}