forked from Juninko/RingBuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RingBuffer.java
164 lines (149 loc) · 5.55 KB
/
RingBuffer.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
package rtc.core.net;
import java.util.Arrays;
public class RingBuffer {
private byte[] array;
private int readPos;//读的起始位置
private int writePos;//写的起始位置
public RingBuffer(int capacity){
array = new byte[capacity];
}
public void clear(){
readPos = writePos = 0;
}
public int remaining(){
if(readPos <= writePos){
return array.length - (writePos - readPos);
}else{
return readPos - writePos;
}
}
public int readable(){
return array.length - remaining();
}
private int reCapacity(int need){
if(need <= array.length){
return array.length;
}
int newCap = array.length;
while (newCap < need){
newCap *= 2;
}
byte[] temp = new byte[newCap];
if(readPos == writePos){
readPos = 0;
writePos = 0;
array = temp;
}else if(readPos < writePos){
int length = readable();
System.arraycopy(array,readPos,temp,0,length);
readPos = 0;
writePos = readPos + length;
array = temp;
}else{
int length = array.length - readPos;
System.arraycopy(array,readPos,temp,0,length);
System.arraycopy(array,0,temp,length,writePos);
writePos = length + writePos;
readPos = 0;
array = temp;
}
return array.length;
}
public int write(byte[] data){
return write(data,0,data.length);
}
public int write(byte[] data,int offset,int length){
if(data == null || data.length == 0 || length == 0) return 0;
if(offset < 0 || length < 0 || (offset + length > data.length)){
throw new IllegalArgumentException(String.format("params error , offset = %s,length = %s,data.length = %s",offset,length,data.length));
}
int remaining = remaining();
if(length > remaining){
reCapacity(array.length + (length - remaining));
}
if(readPos <= writePos){
int wrote = Math.min(array.length - writePos,length);
System.arraycopy(data,offset,array,writePos,wrote);
writePos += wrote;
if(length > wrote){
System.arraycopy(data,offset + wrote,array,0,length - wrote);
writePos = (length - wrote);
}
}else{
System.arraycopy(data,offset,array,writePos,length);
writePos += length;
}
return length;
}
public int peek(byte[] buff){
return peek(buff,0,buff.length);
}
public int peek(byte[] buff,int offset,int length){
return read(buff,offset,length,true);
}
public int read(byte[] buff){
return read(buff,0,buff.length,false);
}
public int read(byte[] buff,int offset,int length,boolean peek){
if(buff == null || buff.length == 0 || length == 0) return 0;
if(offset < 0 || length < 0 || offset >= buff.length){
throw new IllegalArgumentException(String.format("params error , offset = %s,length = %s,buff.length = %s",offset,length,buff.length));
}
length = Math.min(readable(),length);
length = Math.min(buff.length - offset,length);
if(length > 0){
if(readPos <= writePos){
System.arraycopy(array,readPos,buff,offset,length);
if(!peek){
readPos += length;
}
}else{
int read = Math.min(array.length - readPos,length);
System.arraycopy(array,readPos,buff,offset,read);
if(!peek){
readPos += read;
}
if(read < length){
System.arraycopy(array,0,buff,offset + read,length - read);
if(!peek){
readPos = (length - read);
}
}
}
}
return length;
}
public String dump(){
String msg = "array.length = "+array.length + ",";
msg += ("readPos = "+readPos+",");
msg += ("writePos = "+writePos+",");
msg += ("readable = "+readable()+",");
msg += ("remaining = "+remaining());
return msg;
}
public static void main(String[] args){
final RingBuffer buffer = new RingBuffer(8);
final byte[] data = new byte[]{'0','1','2','3','4','5','6','7','8','9'};
buffer.write(data);
System.out.println("write data : "+Arrays.toString(data));
System.out.println("dump : "+buffer.dump());
for(int i = 0 ; i < 100 ; i++){
int readable = buffer.readable();
System.out.println("i = "+ i +",readable = "+buffer.readable());
if(i % 3 == 0){
byte[] readBuff = new byte[readable];
buffer.read(readBuff);
System.out.println("i = " + i+",readBuff = "+ Arrays.toString(readBuff));
System.out.println("dump : "+buffer.dump());
}else{
byte[] temp = data.clone();
if(i % 2 == 0){
temp = data.clone().clone();
}
System.out.println("i = " + i+",write data : "+Arrays.toString(temp));
buffer.write(temp);
System.out.println("dump : "+buffer.dump());
}
}
}
}