forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsciiOutputStream.java
More file actions
45 lines (39 loc) · 1.13 KB
/
AsciiOutputStream.java
File metadata and controls
45 lines (39 loc) · 1.13 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
package com.jsoniter.output;
import java.io.IOException;
import java.io.OutputStream;
class AsciiOutputStream extends OutputStream {
private char[] buf = new char[4096];
private int count = 0;
@Override
public void write(byte[] b, int off, int len) throws IOException {
int i = off;
for (; ; ) {
for (; i < off + len && count < buf.length; i++) {
buf[count++] = (char) b[i];
}
if (count == buf.length) {
char[] newBuf = new char[buf.length * 2];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
} else {
break;
}
}
}
@Override
public void write(int b) throws IOException {
if (count == buf.length) {
char[] newBuf = new char[buf.length * 2];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
}
buf[count++] = (char) b;
}
@Override
public String toString() {
return new String(buf, 0, count);
}
public void reset() {
count = 0;
}
}