14
14
import org .perl6 .nqp .runtime .ExceptionHandling ;
15
15
import org .perl6 .nqp .runtime .ThreadContext ;
16
16
17
- public abstract class SyncHandle implements IIOClosable , IIOEncodable ,
17
+ public abstract class SyncHandle implements IIOClosable , IIOEncodable ,
18
18
IIOSyncReadable , IIOSyncWritable , IIOLineSeparable {
19
-
19
+
20
20
protected ByteChannel chan ;
21
21
protected CharsetEncoder enc ;
22
22
protected CharsetDecoder dec ;
23
23
protected boolean eof = false ;
24
24
protected ByteBuffer readBuffer ;
25
25
protected byte [] linesep = null ;
26
-
26
+
27
27
public void close (ThreadContext tc ) {
28
28
try {
29
29
chan .close ();
30
30
} catch (IOException e ) {
31
31
throw ExceptionHandling .dieInternal (tc , e );
32
32
}
33
33
}
34
-
34
+
35
35
public void setEncoding (ThreadContext tc , Charset cs ) {
36
36
enc = cs .newEncoder ();
37
37
dec = cs .newDecoder ();
38
38
}
39
-
39
+
40
40
public synchronized String slurp (ThreadContext tc ) {
41
41
try {
42
42
// Read in file.
@@ -56,19 +56,19 @@ public synchronized String slurp(ThreadContext tc) {
56
56
total += read ;
57
57
}
58
58
eof = true ;
59
-
59
+
60
60
return decodeBuffers (buffers , total );
61
61
} catch (IOException e ) {
62
62
throw ExceptionHandling .dieInternal (tc , e );
63
63
}
64
64
}
65
-
65
+
66
66
public synchronized String readline (ThreadContext tc ) {
67
67
try {
68
68
boolean foundLine = false ;
69
69
ArrayList <ByteBuffer > lineChunks = new ArrayList <ByteBuffer >();
70
70
int total = 0 ;
71
-
71
+
72
72
while (!foundLine ) {
73
73
/* Ensure we have a buffer available. */
74
74
if (readBuffer == null ) {
@@ -82,7 +82,7 @@ public synchronized String readline(ThreadContext tc) {
82
82
}
83
83
readBuffer .flip ();
84
84
}
85
-
85
+
86
86
/* Look for a line end. */
87
87
int start = readBuffer .position ();
88
88
int end = start ;
@@ -120,18 +120,18 @@ else if (cur == '\r') {
120
120
end ++;
121
121
}
122
122
}
123
-
123
+
124
124
/* Copy what we found into the results. */
125
125
byte [] lineBytes = new byte [end - start ];
126
126
readBuffer .get (lineBytes );
127
127
lineChunks .add (ByteBuffer .wrap (lineBytes ));
128
128
total += lineBytes .length ;
129
-
129
+
130
130
/* If we didn't find a line, will cross chunk boundary. */
131
131
if (!foundLine )
132
132
readBuffer = null ;
133
133
}
134
-
134
+
135
135
if (lineChunks .size () == 1 )
136
136
return dec .decode (lineChunks .get (0 )).toString ();
137
137
else
@@ -140,7 +140,7 @@ else if (cur == '\r') {
140
140
throw ExceptionHandling .dieInternal (tc , e );
141
141
}
142
142
}
143
-
143
+
144
144
private String decodeBuffers (ArrayList <ByteBuffer > buffers , int total ) throws IOException {
145
145
// Copy to a single buffer and decode (could be smarter, but need
146
146
// to be wary as UTF-8 chars may span a buffer boundary).
@@ -153,7 +153,7 @@ private String decodeBuffers(ArrayList<ByteBuffer> buffers, int total) throws IO
153
153
allBytes .rewind ();
154
154
return dec .decode (allBytes ).toString ();
155
155
}
156
-
156
+
157
157
public synchronized String getc (ThreadContext tc ) {
158
158
try {
159
159
int maxBytes = (int )enc .maxBytesPerChar ();
@@ -175,11 +175,11 @@ public synchronized String getc(ThreadContext tc) {
175
175
toDecode .position (0 );
176
176
dec .decode (toDecode , decoded , true ).throwException ();
177
177
return decoded .toString ();
178
- }
178
+ }
179
179
}
180
180
readBuffer .flip ();
181
181
}
182
-
182
+
183
183
/* Get a character from the read buffer. */
184
184
toDecode .position (i );
185
185
toDecode .put (readBuffer .get ());
@@ -200,11 +200,11 @@ public synchronized String getc(ThreadContext tc) {
200
200
throw ExceptionHandling .dieInternal (tc , e );
201
201
}
202
202
}
203
-
203
+
204
204
public boolean eof (ThreadContext tc ) {
205
205
return eof ;
206
206
}
207
-
207
+
208
208
public byte [] read (ThreadContext tc , int bytes ) {
209
209
try {
210
210
ByteBuffer buffer = ByteBuffer .allocate (bytes );
@@ -217,12 +217,12 @@ public byte[] read(ThreadContext tc, int bytes) {
217
217
throw ExceptionHandling .dieInternal (tc , e );
218
218
}
219
219
}
220
-
220
+
221
221
public long write (ThreadContext tc , byte [] array ) {
222
222
ByteBuffer buffer = ByteBuffer .wrap (array );
223
223
return write (tc , buffer );
224
224
}
225
-
225
+
226
226
protected long write (ThreadContext tc , ByteBuffer buffer ) {
227
227
try {
228
228
int toWrite = buffer .limit ();
@@ -233,9 +233,9 @@ protected long write(ThreadContext tc, ByteBuffer buffer) {
233
233
return written ;
234
234
} catch (IOException e ) {
235
235
throw ExceptionHandling .dieInternal (tc , e );
236
- }
236
+ }
237
237
}
238
-
238
+
239
239
public long print (ThreadContext tc , String s ) {
240
240
try {
241
241
ByteBuffer buffer = enc .encode (CharBuffer .wrap (s ));
@@ -244,7 +244,7 @@ public long print(ThreadContext tc, String s) {
244
244
throw ExceptionHandling .dieInternal (tc , e );
245
245
}
246
246
}
247
-
247
+
248
248
public long say (ThreadContext tc , String s ) {
249
249
long bytes = print (tc , s );
250
250
bytes += print (tc , System .lineSeparator ());
0 commit comments