forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularByteBuffer.java
More file actions
166 lines (126 loc) · 4.21 KB
/
Copy pathCircularByteBuffer.java
File metadata and controls
166 lines (126 loc) · 4.21 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
package modern.challenge;
public class CircularByteBuffer {
private int capacity;
private byte[] buffer;
private int readPointer;
private int writePointer;
private int available;
CircularByteBuffer(int capacity) {
this.capacity = capacity;
buffer = new byte[capacity];
}
public synchronized int available() {
return available;
}
public synchronized int capacity() {
return capacity;
}
public synchronized int slots() {
return capacity - available;
}
public synchronized int get() {
if (available == 0) {
return -1;
}
byte value = buffer[readPointer];
readPointer = (readPointer + 1) % capacity;
available--;
return value;
}
public synchronized boolean put(int value) {
if (available == capacity) {
return false;
}
buffer[writePointer] = (byte) value;
writePointer = (writePointer + 1) % capacity;
available++;
return true;
}
public int get(byte[] dest) {
return get(dest, 0, dest.length);
}
public int put(byte[] source) {
return put(source, 0, source.length);
}
public synchronized int get(byte[] dest, int offset, int len) {
if (available == 0) {
return 0;
}
int maxPointer = capacity;
if (readPointer < writePointer) {
maxPointer = writePointer;
}
int countBytes = Math.min(maxPointer - readPointer, len);
System.arraycopy(buffer, readPointer, dest, offset, countBytes);
readPointer = readPointer + countBytes;
if (readPointer == capacity) {
int remainingBytes = Math.min(len - countBytes, writePointer);
if (remainingBytes > 0) {
System.arraycopy(buffer, 0, dest, offset + countBytes, remainingBytes);
readPointer = remainingBytes;
countBytes = countBytes + remainingBytes;
} else {
readPointer = 0;
}
}
available = available - countBytes;
return countBytes;
}
public synchronized int put(byte[] source, int offset, int len) {
if (available == capacity) {
return 0;
}
int maxPointer = capacity;
if (writePointer < readPointer) {
maxPointer = readPointer;
}
int countBytes = Math.min(maxPointer - writePointer, len);
System.arraycopy(source, offset, buffer, writePointer, countBytes);
writePointer = writePointer + countBytes;
if (writePointer == capacity) {
int remainingBytes = Math.min(len - countBytes, readPointer);
if (remainingBytes > 0) {
System.arraycopy(source, offset + countBytes, buffer, 0, remainingBytes);
writePointer = remainingBytes;
countBytes = countBytes + remainingBytes;
} else {
writePointer = 0;
}
}
available = available + countBytes;
return countBytes;
}
public synchronized int skip(int skipBytes) {
if (skipBytes > available) {
skipBytes = available;
}
readPointer = (readPointer + skipBytes) % capacity;
available = available - skipBytes;
return skipBytes;
}
public synchronized int peek() {
if (available > 0) {
return buffer[readPointer];
}
return -1;
}
public synchronized void resize() {
byte[] newBuffer = new byte[capacity * 2];
if (readPointer < writePointer) {
System.arraycopy(buffer, readPointer, newBuffer, 0, available);
} else {
int bytesToCopy = capacity - readPointer;
System.arraycopy(buffer, readPointer, newBuffer, 0, bytesToCopy);
System.arraycopy(buffer, 0, newBuffer, bytesToCopy, writePointer);
}
buffer = newBuffer;
capacity = buffer.length;
readPointer = 0;
writePointer = available;
}
public synchronized void clear() {
readPointer = 0;
writePointer = 0;
available = 0;
}
}