1 11 12 package org.eclipse.jface.text.rules; 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.jface.text.BadLocationException; 17 import org.eclipse.jface.text.IDocument; 18 19 24 public class BufferedRuleBasedScanner extends RuleBasedScanner { 25 26 27 private final static int DEFAULT_BUFFER_SIZE= 500; 28 29 private int fBufferSize= DEFAULT_BUFFER_SIZE; 30 31 private char[] fBuffer= new char[DEFAULT_BUFFER_SIZE]; 32 33 private int fStart; 34 35 private int fEnd; 36 37 private int fDocumentLength; 38 39 40 44 protected BufferedRuleBasedScanner() { 45 super(); 46 } 47 48 55 public BufferedRuleBasedScanner(int size) { 56 super(); 57 setBufferSize(size); 58 } 59 60 65 protected void setBufferSize(int size) { 66 Assert.isTrue(size > 0); 67 fBufferSize= size; 68 fBuffer= new char[size]; 69 } 70 71 77 private void shiftBuffer(int offset) { 78 79 fStart= offset; 80 fEnd= fStart + fBufferSize; 81 if (fEnd > fDocumentLength) 82 fEnd= fDocumentLength; 83 84 try { 85 86 String content= fDocument.get(fStart, fEnd - fStart); 87 content.getChars(0, fEnd - fStart, fBuffer, 0); 88 89 } catch (BadLocationException x) { 90 } 91 } 92 93 96 public void setRange(IDocument document, int offset, int length) { 97 98 super.setRange(document, offset, length); 99 100 fDocumentLength= document.getLength(); 101 shiftBuffer(offset); 102 } 103 104 107 public int read() { 108 109 if (fOffset >= fRangeEnd) { 110 ++ fOffset; 111 return EOF; 112 } 113 114 if (fOffset == fEnd) 115 shiftBuffer(fEnd); 116 else if (fOffset < fStart || fEnd < fOffset) 117 shiftBuffer(fOffset); 118 119 return fBuffer[fOffset++ - fStart]; 120 } 121 122 125 public void unread() { 126 127 if (fOffset == fStart) 128 shiftBuffer(Math.max(0, fStart - (fBufferSize / 2))); 129 130 -- fOffset; 131 } 132 } 133 134 135 | Popular Tags |