アルファ版。新版→https://osdn.jp/users/tacticsrealize/pf/ChlorophyllUploader/wiki/FrontPage
Révision | 2cc40d8f37a47e4a1c86ef6d62685f2a726efab0 |
---|---|
Taille | 1,530 octets |
l'heure | 2015-06-26 17:39:33 |
Auteur | |
Message de Log | add project |
package mirrg.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.function.Consumer;
public class UTF8StreamReader
{
private static final Charset charset = Charset.forName("UTF-8");
private InputStream in;
private boolean blocking;
public UTF8StreamReader(InputStream in, boolean blocking)
{
this.in = in;
this.blocking = blocking;
}
private ArrayList<Byte> buffer = new ArrayList<>();
public void stream(Consumer<String> consumer)
{
boolean r = false;
while (true) {
int b;
try {
b = in.read();
//System.out.println((char)b); // TODO
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (b < 0) {
if (blocking) {
flush(consumer);
break;
}
} else if ((char) b == '\r') {
r = true;
flush(consumer);
buffer.clear();
continue;
} else if ((char) b == '\n') {
if (r) {
} else {
flush(consumer);
buffer.clear();
}
continue;
} else {
buffer.add((byte) b);
continue;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
private void flush(Consumer<String> consumer)
{
byte[] buffer2 = new byte[buffer.size()];
for (int i = 0; i < buffer2.length; i++) {
buffer2[i] = buffer.get(i);
}
consumer.accept(new String(buffer2, charset));
}
}