package bluej.parser;

import java.io.Reader;

import javax.swing.text.Document;
import javax.swing.text.Segment;

import bluej.editor.moe.MoeSyntaxDocument;
import bluej.utility.Debug;
import threadchecker.OnThread;
import threadchecker.Tag;


| An efficient reader which reads directly from the supplied Document. | | @author Davin McCall | public class DocumentReader extends Reader{ private Segment buffer; private MoeSyntaxDocument document; private int bufpos; private int docPosition; private int docLength;
| Construct a DocumentReader to read an entire document. | public DocumentReader(MoeSyntaxDocument document) { this(document, 0); }
| Construct a DocumentReader to read a document starting from the given position. | public DocumentReader(MoeSyntaxDocument document, int position) { buffer = new Segment(); buffer.setPartialReturn(true); this.document = document; docPosition = position; docLength = document.getLength(); fillBuffer(); }
| Construct a new DocumentReader to read text between the two | given document positions. | public DocumentReader(MoeSyntaxDocument document, int position, int endpos) { buffer = new Segment(); buffer.setPartialReturn(true); this.document = document; docPosition = position; docLength = endpos; if (docLength > document.getLength()) throw new IllegalArgumentException("Trying to construct DocumentReader to look up to " + endpos + " in a document of length: " + document.getLength()); fillBuffer(); } @Override @OnThread(value = Tag.FXPlatform,ignoreParent = true) public void close() { } @Override @OnThread(value = Tag.FXPlatform,ignoreParent = true) public int read() { if (bufpos == buffer.getEndIndex()) { if (docPosition == docLength) { return -1; } fillBuffer(); } return buffer.array[bufpos++]; } @Override @OnThread(value = Tag.FXPlatform,ignoreParent = true) public int read(char[] cbuf, int off, int len) { int docAvail = Math.min(len, docLength - docPosition + buffer.getEndIndex() - bufpos); if (docAvail == 0) { return -1; } len = Math.min(len, docAvail); int remaining = len; while (remaining > 0){ int avail = Math.min(buffer.getEndIndex() - bufpos, remaining); if (avail == 0) { fillBuffer(); avail = Math.min(buffer.getEndIndex() - bufpos, remaining); } System.arraycopy(buffer.array, bufpos, cbuf, off, avail); off += avail; bufpos += avail; remaining -= avail; } return len; } private void fillBuffer() { int docAvail = docLength - docPosition; document.getText(docPosition, docAvail, buffer); docPosition += (buffer.getEndIndex() - buffer.getBeginIndex()); bufpos = buffer.getBeginIndex(); } }
top, use, map, class DocumentReader

.   DocumentReader
.   DocumentReader
.   DocumentReader
.   close
.   read
.   read
.   fillBuffer




142 neLoCode + 6 LoComm