LineInputStream.java
4.22 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.jcr.vault.util;
import java.io.IOException;
import java.io.InputStream;
public class LineInputStream
extends InputStream {
public static final byte[] LS_UNIX = new byte[]{10};
public static final byte[] LS_WINDOWS = new byte[]{13, 10};
public static final byte[] LS_NATIVE = System.getProperty("line.separator").getBytes();
private byte[] buffer = new byte[8192];
private byte[] lineFeed = LS_NATIVE;
private byte[] lineSpool;
private int pos = 0;
private int end = 0;
private static final char STATE_INIT = ' ';
private static final char STATE_CR = 'c';
private static final char STATE_LF = 'l';
private static final char STATE_CRLF = 'f';
private char state = 32;
boolean isEof = false;
private byte[] spool;
private int spoolPos = 0;
private final InputStream in;
public LineInputStream(InputStream in, byte[] ls) {
this.in = in;
if (ls != null) {
this.lineFeed = ls;
}
this.lineSpool = new byte[this.lineFeed.length + 1];
System.arraycopy(this.lineFeed, 0, this.lineSpool, 0, this.lineFeed.length);
}
private int fillBuffer() throws IOException {
int ret = this.in.read(this.buffer, this.end, this.buffer.length - this.end);
if (ret >= 0) {
this.end += ret;
} else {
this.isEof = true;
}
return ret;
}
public int read() throws IOException {
byte[] one = new byte[1];
if (this.read(one) == -1) {
return -1;
}
return one[0];
}
public int read(byte[] b, int off, int len) throws IOException {
if (this.isEof && this.spool == null) {
if (this.state != ' ') {
this.spool = this.lineFeed;
this.state = 32;
} else {
return -1;
}
}
int total = 0;
while (total < len) {
byte c;
if (this.spool != null) {
b[off + total++] = this.spool[this.spoolPos++];
if (this.spoolPos != this.spool.length) continue;
this.spool = null;
this.spoolPos = 0;
continue;
}
if (this.pos == this.end) {
int ret = this.fillBuffer();
if (ret == 0 && this.pos == this.end) {
this.end = 0;
this.pos = 0;
continue;
}
if (ret == -1) break;
}
if ((c = this.buffer[this.pos++]) == 10) {
switch (this.state) {
case ' ': {
this.state = 108;
break;
}
case 'c': {
this.state = 102;
break;
}
case 'l': {
this.spool = this.lineFeed;
break;
}
case 'f': {
this.spool = this.lineFeed;
this.state = 108;
}
}
continue;
}
if (c == 13) {
switch (this.state) {
case ' ': {
this.state = 99;
break;
}
case 'l': {
this.state = 102;
break;
}
case 'c': {
this.spool = this.lineFeed;
break;
}
case 'f': {
this.spool = this.lineFeed;
this.state = 99;
}
}
continue;
}
if (this.state != ' ') {
this.spool = this.lineSpool;
this.lineSpool[this.lineSpool.length - 1] = c;
this.state = 32;
continue;
}
b[off + total++] = c;
}
return total;
}
public void close() throws IOException {
this.in.close();
}
}