BufferedReaderEx
テキストファイルのキーワード置換処理。
まあ単純な処理だ。
BufferedReaderで1行ずつ読んで置換してBufferedWriterに出力。
しかし、改行コードがバラバラのファイルが数百個あるとなると?
もちろん置換結果のファイルは元のファイルと同じように改行していなければいけない。
BufferedReaderは改行コードがCR/LF/CRLFのどれでも関係なく認識してくれるのでただ読む分には便利だが、改行コードが何なのかがわからない。
だもんで、到達した改行コードが取得できるものをBufferedReaderのソースを流用して作ってみた。
/** * BufferedReaderの改造版。<br/> * {@link #readLine()}が到達した改行コードを{@link #getLineSeparator()}で取得できる。<br/> * 使用例: * <pre> * BufferedReaderEx br = new BufferedReaderEx(new StringReader("aaa\rbbb\nccc\r\nd")); * String text = null; * while ((text = br.readLine()) != null) { * System.out.print(text); * switch(br.getLineSeparator()) { * case CR: * System.out.println("(CR)"); * break; * case LF: * System.out.println("(LF)"); * break; * case CRLF: * System.out.println("(CRLF)"); * break; * default: * System.out.println("()"); * break; * } * } * </pre> */ public class BufferedReaderEx extends Reader { private Reader in; private char cb[]; private int nChars, nextChar; private static final int INVALIDATED = -2; private static final int UNMARKED = -1; private int markedChar = UNMARKED; private int readAheadLimit = 0; private static int defaultCharBufferSize = 8192; private static int defaultExpectedLineLength = 80; // 改行コード private LineSeparator lineSeparator = LineSeparator.Nothing; public enum LineSeparator { CR("\r"), LF("\n"), CRLF("\r\n"), Nothing(""); private String separator; private LineSeparator(String separator) { this.separator = separator; } @Override public String toString() { return separator; } } public BufferedReaderEx(Reader in, int sz) { super(in); if (sz <= 0) throw new IllegalArgumentException("Buffer size <= 0"); this.in = in; cb = new char[sz]; nextChar = nChars = 0; } public BufferedReaderEx(Reader in) { this(in, defaultCharBufferSize); } private void ensureOpen() throws IOException { if (in == null) throw new IOException("Stream closed"); } private void fill() throws IOException { int dst; if (markedChar <= UNMARKED) { dst = 0; } else { int delta = nextChar - markedChar; if (delta >= readAheadLimit) { markedChar = INVALIDATED; readAheadLimit = 0; dst = 0; } else { if (readAheadLimit <= cb.length) { System.arraycopy(cb, markedChar, cb, 0, delta); markedChar = 0; dst = delta; } else { char ncb[] = new char[readAheadLimit]; System.arraycopy(cb, markedChar, ncb, 0, delta); cb = ncb; markedChar = 0; dst = delta; } nextChar = nChars = delta; } } int n; do { n = in.read(cb, dst, cb.length - dst); } while (n == 0); if (n > 0) { nChars = dst + n; nextChar = dst; } } /** * このメソッドを使用すると{@link #getLineSeparator()}は{@link LineSeparator#Nothing}を返します。 * @see Reader#read() */ public int read() throws IOException { synchronized (lock) { ensureOpen(); lineSeparator = LineSeparator.Nothing; for (;;) { if (nextChar >= nChars) { fill(); if (nextChar >= nChars) return -1; } return cb[nextChar++]; } } } private int read1(char[] cbuf, int off, int len) throws IOException { if (nextChar >= nChars) { if (len >= cb.length && markedChar <= UNMARKED) { return in.read(cbuf, off, len); } fill(); } if (nextChar >= nChars) return -1; int n = Math.min(len, nChars - nextChar); System.arraycopy(cb, nextChar, cbuf, off, n); nextChar += n; return n; } /** * このメソッドを使用すると{@link #getLineSeparator()}は{@link LineSeparator#Nothing}を返します。 * @see Reader#read(char[], int, int) */ public int read(char cbuf[], int off, int len) throws IOException { synchronized (lock) { ensureOpen(); lineSeparator = LineSeparator.Nothing; if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int n = read1(cbuf, off, len); if (n <= 0) return n; while ((n < len) && in.ready()) { int n1 = read1(cbuf, off + n, len - n); if (n1 <= 0) break; n += n1; } return n; } } public String readLine() throws IOException { StringBuffer s = null; int startChar; synchronized (lock) { ensureOpen(); lineSeparator = LineSeparator.Nothing; bufferLoop: for (;;) { if (nextChar >= nChars) fill(); if (nextChar >= nChars) { if (s != null && s.length() > 0) return s.toString(); else return null; } boolean eol = false; char c = 0; int i; charLoop: for (i = nextChar; i < nChars; i++) { c = cb[i]; if ((c == '\n') || (c == '\r')) { eol = true; break charLoop; } } startChar = nextChar; nextChar = i; if (eol) { String str; if (s == null) { str = new String(cb, startChar, i - startChar); } else { s.append(cb, startChar, i - startChar); str = s.toString(); } nextChar++; if (c == '\r') { // 認識した改行コードを設定する lineSeparator = LineSeparator.CR; // CRの場合LFが続くかもしれないので次の文字を読む // 必要ならソースからバッファに読み込む if (nextChar >= nChars) fill(); if (nextChar < nChars) { if (cb[nextChar] == '\n') { // 認識した改行コードを設定する lineSeparator = LineSeparator.CRLF; // LFをスキップする nextChar++; } } } else if (c =='\n') { // 認識した改行コードを設定する lineSeparator = LineSeparator.LF; } return str; } if (s == null) s = new StringBuffer(defaultExpectedLineLength); s.append(cb, startChar, i - startChar); } } } public long skip(long n) throws IOException { if (n < 0L) { throw new IllegalArgumentException("skip value is negative"); } synchronized (lock) { ensureOpen(); long r = n; while (r > 0) { if (nextChar >= nChars) fill(); if (nextChar >= nChars) break; long d = nChars - nextChar; if (r <= d) { nextChar += r; r = 0; break; } else { r -= d; nextChar = nChars; } } return n - r; } } public boolean ready() throws IOException { synchronized (lock) { ensureOpen(); return (nextChar < nChars) || in.ready(); } } public boolean markSupported() { return true; } public void mark(int readAheadLimit) throws IOException { if (readAheadLimit < 0) { throw new IllegalArgumentException("Read-ahead limit < 0"); } synchronized (lock) { ensureOpen(); this.readAheadLimit = readAheadLimit; markedChar = nextChar; } } public void reset() throws IOException { synchronized (lock) { ensureOpen(); if (markedChar < 0) throw new IOException((markedChar == INVALIDATED) ? "Mark invalid" : "Stream not marked"); nextChar = markedChar; } } public void close() throws IOException { synchronized (lock) { if (in == null) return; in.close(); in = null; cb = null; } } /** * {@link #readLine()}が到達した改行コードを返す。 * 改行コードがなくソース終端に到達した場合は{@link LineSeparator#Nothing}を返す。<br/> * {@link #readLine()}呼び出し前は{@link LineSeparator#Nothing}を返す。 * @return */ public LineSeparator getLineSeparator() { return lineSeparator; } }