1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 57 69 public class PositionReader extends FilterReader { 70 71 protected PositionReader mPosReader; 72 73 protected int mPosition = 0; 74 75 private boolean mClosed = false; 76 77 public PositionReader(Reader reader) { 78 super(reader); 79 if (reader instanceof PositionReader) { 80 mPosReader = (PositionReader)reader; 81 } 82 } 83 84 87 public int getNextPosition() { 88 return mPosition; 89 } 90 91 public int read() throws IOException { 92 try { 93 int c; 94 if ((c = in.read()) != -1) { 95 if (mPosReader == null) { 96 mPosition++; 97 } 98 else { 99 mPosition = mPosReader.getNextPosition(); 100 } 101 } 102 else { 103 close(); 104 } 105 106 return c; 107 } 108 catch (IOException e) { 109 if (mClosed) { 110 return -1; 111 } 112 else { 113 throw e; 114 } 115 } 116 } 117 118 public int read(char[] buf, int off, int length) throws IOException { 119 int i = 0; 120 while (i < length) { 121 int c; 122 if ((c = read()) == -1) { 123 return (i == 0)? -1 : i; 124 } 125 buf[i++ + off] = (char)c; 126 } 127 128 return i; 129 } 130 131 public void close() throws IOException { 132 mClosed = true; 133 super.close(); 134 } 135 } 136 | Popular Tags |