Skip to content

Instantly share code, notes, and snippets.

@stebulus
Created June 23, 2014 18:05
Show Gist options
  • Select an option

  • Save stebulus/63051af547bc5060910f to your computer and use it in GitHub Desktop.

Select an option

Save stebulus/63051af547bc5060910f to your computer and use it in GitHub Desktop.
import java.io.Reader;
import com.fasterxml.aalto.in.XmlScanner;
import com.fasterxml.aalto.in.ReaderConfig;
public class BufErr {
public static void main(String[] args) {
try {
Reader rdr = new StringArrayReader(new String[] {
"<foo><bar>text<",
"/",
"bar></foo>"
});
ReaderConfig cfg = new ReaderConfig();
cfg.doCoalesceText(true);
XmlScanner scan = new ReaderScanner(cfg, rdr);
while (scan.nextFromTree() != XmlScanner.TOKEN_EOI) {
// nop
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.Reader;
public class StringArrayReader extends Reader {
private String[] chunks;
private int chunk, pos;
public StringArrayReader(String[] chunks) {
this.chunks = chunks;
chunk = pos = 0;
}
public void close() {}
public int read(char[] cbuf, int off, int len) {
if (chunk >= chunks.length)
return -1;
while (pos >= chunks[chunk].length()) {
chunk++;
pos = 0;
if (chunk >= chunks.length)
return -1;
}
len = Math.min(len, chunks[chunk].length() - pos);
for (int i = 0; i < len; i++)
cbuf[off+i] = chunks[chunk].charAt(pos+i);
pos += len;
return len;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment