@@ -940,9 +940,15 @@ public Charset getEncoding() {
940
940
protected class CS extends AbstractCS implements ICharacterStream .Readable .Buffered {
941
941
private int buffer = 0 ;
942
942
private int bufferIndex = 0 ;
943
+ private int back = -1 ;
943
944
944
945
@ Override
945
946
public char read () throws EOFException {
947
+ if (back != -1 ) {
948
+ char c = (char )back ;
949
+ back = -1 ;
950
+ return c ;
951
+ }
946
952
if (strings == null ) throw new EOFException ();
947
953
while (buffer <= lastUsed && bufferIndex == strings [buffer ].length ()) {
948
954
buffer ++;
@@ -954,8 +960,17 @@ public char read() throws EOFException {
954
960
955
961
@ Override
956
962
public int readSync (char [] buf , int offset , int length ) {
957
- if (strings == null ) return - 1 ;
963
+ if (length <= 0 ) return 0 ;
958
964
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 ;
959
974
do {
960
975
while (buffer <= lastUsed && bufferIndex == strings [buffer ].length ()) {
961
976
buffer ++;
@@ -976,6 +991,11 @@ public int readSync(char[] buf, int offset, int length) {
976
991
977
992
@ Override
978
993
public int readAsync () {
994
+ if (back != -1 ) {
995
+ char c = (char )back ;
996
+ back = -1 ;
997
+ return c ;
998
+ }
979
999
if (strings == null ) return -1 ;
980
1000
while (buffer <= lastUsed && bufferIndex == strings [buffer ].length ()) {
981
1001
buffer ++;
@@ -1002,6 +1022,11 @@ public AsyncSupplier<UnprotectedString, IOException> readNextBufferAsync() {
1002
1022
1003
1023
@ Override
1004
1024
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
+ }
1005
1030
if (strings == null ) return null ;
1006
1031
while (buffer <= lastUsed && bufferIndex == strings [buffer ].length ()) {
1007
1032
buffer ++;
@@ -1016,6 +1041,13 @@ public UnprotectedString readNextBuffer() {
1016
1041
1017
1042
@ Override
1018
1043
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
+ }
1019
1051
if (strings == null ) return false ;
1020
1052
do {
1021
1053
while (buffer <= lastUsed && bufferIndex == strings [buffer ].length ()) {
@@ -1054,26 +1086,12 @@ public AsyncSupplier<Boolean, IOException> readUntilAsync(char endChar, Unprotec
1054
1086
1055
1087
@ Override
1056
1088
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 ;
1072
1090
}
1073
1091
1074
1092
@ Override
1075
1093
public boolean endReached () {
1076
- return strings == null || buffer > lastUsed ;
1094
+ return back == - 1 && ( strings == null || buffer > lastUsed ) ;
1077
1095
}
1078
1096
1079
1097
@ Override
0 commit comments