1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 57 64 public class LinePositionReader extends PositionReader { 65 private int mLineNumber = 1; 66 private int mPushback = -1; 67 68 public LinePositionReader(Reader reader) { 69 super(reader); 70 } 71 72 public int read() throws IOException { 73 int c; 74 75 if (mPushback >= 0) { 76 c = mPushback; 77 mPushback = -1; 78 } 79 else { 80 c = super.read(); 81 } 82 83 if (c == '\n') { 84 mLineNumber++; 85 } 86 else if (c == '\r') { 87 int peek = super.read(); 88 if (peek != '\n') { 89 mLineNumber++; 90 } 91 mPushback = peek; 92 } 93 94 return c; 95 } 96 97 101 public String readLine() throws IOException { 102 StringBuffer buf = new StringBuffer (80); 103 int line = mLineNumber; 104 int c; 105 while (line == mLineNumber && (c = read()) >= 0) { 106 buf.append((char)c); 107 } 108 return buf.toString(); 109 } 110 111 118 public int skipForwardToLine(int line) throws IOException { 119 while (mLineNumber < line && read() >= 0) {} 120 return mLineNumber; 121 } 122 123 127 public int getLineNumber() { 128 return mLineNumber; 129 } 130 131 135 public static String cleanWhitespace(String str) { 136 int length = str.length(); 137 138 StringBuffer buf = new StringBuffer (length); 139 for (int i=0; i<length; i++) { 140 char c = str.charAt(i); 141 if (Character.isWhitespace(c)) { 142 buf.append(' '); 143 } 144 else { 145 buf.append(c); 146 } 147 } 148 149 return buf.toString(); 150 } 151 152 156 public static String createSequence(char c, int length) { 157 if (length < 0) length = 1; 158 159 StringBuffer buf = new StringBuffer (length); 160 for (; length > 0; length--) { 161 buf.append(c); 162 } 163 return buf.toString(); 164 } 165 } 166 | Popular Tags |