cn.allbs.hj212.core.ReaderStream Maven / Gradle / Ivy
package cn.allbs.hj212.core;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.lang.reflect.Field;
import java.nio.CharBuffer;
import java.util.Optional;
import java.util.function.Consumer;
/**
* 功能:
*
* @author chenQi
*/
public class ReaderStream {
private PushbackReader reader;
private int bufSize = 1024;
private ReaderMatch childMatch;
private ParentMatch parentMatch;
public static ReaderStream of() {
return new ReaderStream<>(null);
}
public static ReaderStream of(PushbackReader reader) {
return new ReaderStream<>(reader);
}
public static ReaderStream of(Reader reader) {
return new ReaderStream<>(reader, -0);
}
public static ReaderStream of(Reader reader, int pushBackCount) {
return new ReaderStream<>(reader, pushBackCount);
}
public ReaderStream(PushbackReader reader) {
use(reader);
}
public ReaderStream(Reader reader, int bufSize) {
use(reader, bufSize);
}
public ReaderStream(PushbackReader reader, int bufSize, ParentMatch parentMatch) {
use(reader, bufSize, parentMatch);
}
public ReaderStream(Reader reader, int bufSize, ParentMatch parentMatch) {
use(reader, bufSize, parentMatch);
}
public ReaderStream use(PushbackReader reader) {
Field field = null;
try {
field = reader.getClass().getDeclaredField("buf");
field.setAccessible(true);
char[] chars = (char[]) field.get(reader);
if (chars.length < 1) {
this.reader = new PushbackReader(reader, this.bufSize);
return this;
}
this.bufSize = chars.length;
this.reader = reader;
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
public ReaderStream use(Reader reader) {
return use(reader, -0);
}
public ReaderStream use(Reader reader, int bufSize) {
if (bufSize > 0) {
this.bufSize = bufSize;
}
this.reader = new PushbackReader(reader, this.bufSize);
return this;
}
public ReaderStream use(PushbackReader reader, int bufSize, ParentMatch parentMatch) {
if (bufSize < 1) {
this.reader = new PushbackReader(reader, this.bufSize);
this.parentMatch = parentMatch;
return this;
}
this.bufSize = bufSize;
this.reader = reader;
this.parentMatch = parentMatch;
return this;
}
public ReaderStream use(Reader reader, int bufSize, ParentMatch parentMatch) {
if (bufSize > 0) {
this.bufSize = bufSize;
}
this.reader = new PushbackReader(reader, this.bufSize);
this.parentMatch = parentMatch;
return this;
}
protected PushbackReader reader() {
return reader;
}
protected int bufSize() {
return bufSize;
}
/**
* 接下来的一个字符
*
* @return SingleCharMatch
*/
public SingleCharMatch> next() {
SingleCharMatch> charMatch = new SingleCharMatch<>(this);
this.childMatch = charMatch;
return charMatch;
}
/**
* 接下来的几个字符
*
* @param count 个数
* @return MultipleCharMatch
*/
public MultipleCharMatch> next(int count) {
MultipleCharMatch> charMatch = new MultipleCharMatch<>(this, count);
this.childMatch = charMatch;
return charMatch;
}
public ParentMatch back() {
return parentMatch;
}
public Optional
© 2015 - 2024 Weber Informatics LLC | Privacy Policy