Skip to content

Commit 5f1cf84

Browse files
committed
fix UnprotectedStringBuffer
1 parent 40a2bc9 commit 5f1cf84

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/util/UnprotectedStringBuffer.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,15 @@ public Charset getEncoding() {
940940
protected class CS extends AbstractCS implements ICharacterStream.Readable.Buffered {
941941
private int buffer = 0;
942942
private int bufferIndex = 0;
943+
private int back = -1;
943944

944945
@Override
945946
public char read() throws EOFException {
947+
if (back != -1) {
948+
char c = (char)back;
949+
back = -1;
950+
return c;
951+
}
946952
if (strings == null) throw new EOFException();
947953
while (buffer <= lastUsed && bufferIndex == strings[buffer].length()) {
948954
buffer++;
@@ -954,8 +960,17 @@ public char read() throws EOFException {
954960

955961
@Override
956962
public int readSync(char[] buf, int offset, int length) {
957-
if (strings == null) return -1;
963+
if (length <= 0) return 0;
958964
int done = 0;
965+
if (back != -1) {
966+
buf[offset++] = (char)back;
967+
back = -1;
968+
if (--length <= 0)
969+
return 1;
970+
if (strings == null) return 1;
971+
done = 1;
972+
} else if (strings == null)
973+
return -1;
959974
do {
960975
while (buffer <= lastUsed && bufferIndex == strings[buffer].length()) {
961976
buffer++;
@@ -976,6 +991,11 @@ public int readSync(char[] buf, int offset, int length) {
976991

977992
@Override
978993
public int readAsync() {
994+
if (back != -1) {
995+
char c = (char)back;
996+
back = -1;
997+
return c;
998+
}
979999
if (strings == null) return -1;
9801000
while (buffer <= lastUsed && bufferIndex == strings[buffer].length()) {
9811001
buffer++;
@@ -1002,6 +1022,11 @@ public AsyncSupplier<UnprotectedString, IOException> readNextBufferAsync() {
10021022

10031023
@Override
10041024
public UnprotectedString readNextBuffer() {
1025+
if (back != -1) {
1026+
UnprotectedString s = new UnprotectedString(new char[] { (char)back }, 0, 1, 1);
1027+
back = -1;
1028+
return s;
1029+
}
10051030
if (strings == null) return null;
10061031
while (buffer <= lastUsed && bufferIndex == strings[buffer].length()) {
10071032
buffer++;
@@ -1016,6 +1041,13 @@ public UnprotectedString readNextBuffer() {
10161041

10171042
@Override
10181043
public boolean readUntil(char endChar, UnprotectedStringBuffer string) throws IOException {
1044+
if (back != -1) {
1045+
char c = (char)back;
1046+
back = -1;
1047+
if (c == endChar)
1048+
return true;
1049+
string.append(c);
1050+
}
10191051
if (strings == null) return false;
10201052
do {
10211053
while (buffer <= lastUsed && bufferIndex == strings[buffer].length()) {
@@ -1054,26 +1086,12 @@ public AsyncSupplier<Boolean, IOException> readUntilAsync(char endChar, Unprotec
10541086

10551087
@Override
10561088
public void back(char c) {
1057-
if (strings == null) {
1058-
append(c);
1059-
buffer = 0;
1060-
bufferIndex = 0;
1061-
return;
1062-
}
1063-
while (bufferIndex == 0 && buffer > 0) {
1064-
buffer--;
1065-
bufferIndex = strings[buffer].length();
1066-
}
1067-
if (bufferIndex == 0 && buffer == 0) {
1068-
addFirst(c);
1069-
return;
1070-
}
1071-
strings[buffer].setCharAt(--bufferIndex, c);
1089+
back = c;
10721090
}
10731091

10741092
@Override
10751093
public boolean endReached() {
1076-
return strings == null || buffer > lastUsed;
1094+
return back == -1 && (strings == null || buffer > lastUsed);
10771095
}
10781096

10791097
@Override

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/util/TestUnprotectedStringBufferAsCharacterStreamBuffered.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected ICharacterStream.Readable.Buffered openStream(IO.Readable io) throws E
3333
UnprotectedStringBuffer s = new UnprotectedStringBuffer();
3434
char[] chars = new char[testBuf.length];
3535
for (int i = 0; i < testBuf.length; ++i)
36-
chars[i] = (char)testBuf[i];
36+
chars[i] = (char)(testBuf[i] & 0xFF);
3737
for (int i = 0; i < nbBuf; ++i) {
3838
s.append(new UnprotectedString(chars, 0, chars.length, chars.length));
3939
}

0 commit comments

Comments
 (0)