[29855] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 | package util;
|
---|
| 6 |
|
---|
| 7 | import java.io.BufferedReader;
|
---|
| 8 | import java.io.File;
|
---|
| 9 | import java.io.FileInputStream;
|
---|
| 10 | import java.io.IOException;
|
---|
| 11 | import java.io.InputStreamReader;
|
---|
| 12 | import java.util.NoSuchElementException;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * A simple buffered text file reader which can read characters from a file.
|
---|
| 16 | *
|
---|
| 17 | * @author John
|
---|
| 18 | */
|
---|
| 19 | public class CharReader {
|
---|
| 20 |
|
---|
| 21 | private static final int CHAR_BUFFER_SIZE = 1000;
|
---|
| 22 | private char[] charBuffer;
|
---|
| 23 | private BufferedReader reader;
|
---|
| 24 | private int currentIndex;
|
---|
| 25 | private int lastIndex;
|
---|
| 26 |
|
---|
| 27 | public CharReader(File file, String charsetEncoding) throws IOException {
|
---|
| 28 | reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetEncoding));
|
---|
| 29 | charBuffer = new char[CHAR_BUFFER_SIZE];
|
---|
| 30 | currentIndex = lastIndex = 0;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Returns true if the CharReader has another char in its input.
|
---|
| 35 | * @return true if and only if this CharReader has another char.
|
---|
| 36 | */
|
---|
| 37 | public boolean hasNextChar() {
|
---|
| 38 | if (currentIndex < lastIndex) {
|
---|
| 39 | return true;
|
---|
| 40 | }
|
---|
| 41 | read();
|
---|
| 42 | return currentIndex < lastIndex;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Returns the next char from this CharReader.
|
---|
| 47 | * @return the next char.
|
---|
| 48 | * @throws NoSuchElementException if no more chars are available
|
---|
| 49 | */
|
---|
| 50 | public char nextChar() throws NoSuchElementException {
|
---|
| 51 | checkIndex(currentIndex);
|
---|
| 52 | return charBuffer[currentIndex++];
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Looks at the next char from this CharReader without removing it.
|
---|
| 57 | * @return the next char.
|
---|
| 58 | * @throws NoSuchElementException if no more chars are available
|
---|
| 59 | */
|
---|
| 60 | public char peek() throws NoSuchElementException {
|
---|
| 61 | checkIndex(currentIndex);
|
---|
| 62 | return charBuffer[currentIndex];
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Close the CharReader.
|
---|
| 67 | */
|
---|
| 68 | public void close() {
|
---|
| 69 | try {
|
---|
| 70 | reader.close();
|
---|
| 71 | } catch (IOException e) {
|
---|
| 72 | e.printStackTrace();
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Read a portion of characters into an array.
|
---|
| 78 | */
|
---|
| 79 | private void read() {
|
---|
| 80 | try {
|
---|
| 81 | lastIndex = reader.read(charBuffer, 0, charBuffer.length);
|
---|
| 82 | currentIndex = 0;
|
---|
| 83 | } catch (Exception e) {
|
---|
| 84 | e.printStackTrace();
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Throws a NoSuchElementException if the index is less then 0 or greater
|
---|
| 90 | * then char buffer length.
|
---|
| 91 | * @param index Index to check.
|
---|
| 92 | * @throws NoSuchElementException if the index is less then 0 or greater
|
---|
| 93 | * then the char buffer length.
|
---|
| 94 | */
|
---|
| 95 | private void checkIndex(int index) throws NoSuchElementException {
|
---|
| 96 | if (index < 0 || index >= charBuffer.length) {
|
---|
| 97 | throw new NoSuchElementException();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|