下面列出了java.io.Reader#reset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String markRead(Reader a, int x, int y) throws IOException {
int m = 0;
int r;
StringBuilder builder = new StringBuilder();
do {
m++;
r = a.read();
if (m == x)
a.mark((x + y));
if (m == (x + y))
a.reset();
if (r != -1)
builder.append((char) r);
} while (r != -1);
return builder.toString();
}
public static String markRead(Reader a, int x, int y) throws IOException {
int m = 0;
int r;
StringBuilder builder = new StringBuilder();
do {
m++;
r = a.read();
if (m == x)
a.mark((x + y));
if (m == (x + y))
a.reset();
if (r != -1)
builder.append((char) r);
} while (r != -1);
return builder.toString();
}
private static boolean isCFENCODED(Reader _inFile) throws IOException {
CharArrayWriter buffer = new CharArrayWriter(CFENCODE_HEADER_LEN);
_inFile.mark(CFENCODE_HEADER_LEN);
for (int i = 0; i < CFENCODE_HEADER_LEN; i++) {
buffer.write(_inFile.read());
}
if (buffer.toString().equals(CFENCODE_HEADER)) {
return true;
}
_inFile.reset();
return false;
}
private static int parse(Reader reader) {
try {
reader.mark(1);
int first = reader.read();
reader.reset();
if (HttpParser.isAlpha(first)) {
return HttpParser.readHostDomainName(reader);
} else if (HttpParser.isNumeric(first)) {
return HttpParser.readHostIPv4(reader, false);
} else if ('[' == first) {
return HttpParser.readHostIPv6(reader);
} else {
// Invalid
throw new IllegalArgumentException();
}
} catch (IOException ioe) {
// Should never happen
throw new IllegalArgumentException(ioe);
}
}
public static String markRead(Reader a, int x, int y) throws IOException {
int m = 0;
int r;
StringBuilder builder = new StringBuilder();
do {
m++;
r = a.read();
if (m == x)
a.mark((x + y));
if (m == (x + y))
a.reset();
if (r != -1)
builder.append((char) r);
} while (r != -1);
return builder.toString();
}
private static char[] readCharArray(Reader reader) throws IOException {
if (reader.markSupported()) {
reader.mark(1024);
}
int length = reader.read();
int delimiter = reader.read();
if (delimiter != 0) {
if (reader.markSupported()) {
reader.reset();
}
throw new IOException("Unsupported reply exception");
}
char[] buffer = new char[length];
reader.read(buffer);
return buffer;
}
/**
* Fetch a sample Clob.
* If this method fails, the test fails.
*
* @param con database connection to fetch data from.
* @return a sample <code>Clob</code> object.
*/
public static Clob getSampleClob(Connection con)
throws SQLException {
Reader clobInput = new StringReader(clobData);
PreparedStatement pStmt =
con.prepareStatement("update BLOBCLOB set CLOBDATA = ? where ID = ?");
try {
clobInput.reset();
} catch (IOException ioe) {
fail("Failed to reset clob input stream: " + ioe.getMessage());
}
pStmt.setClob(1, clobInput, clobData.length());
pStmt.setInt(2, ID_SAMPLEVALUES);
assertEquals("Invalid update count", 1, pStmt.executeUpdate());
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where ID = " +
ID_SAMPLEVALUES);
rs.next();
Clob clob = rs.getClob(1);
rs.close();
stmt.close();
return clob;
}
/**
* Fetch a sample Clob.
* If this method fails, the test fails.
*
* @param con database connection to fetch data from.
* @return a sample <code>Clob</code> object.
*/
public static Clob getSampleClob(Connection con)
throws SQLException {
Reader clobInput = new StringReader(clobData);
PreparedStatement pStmt =
con.prepareStatement("update BLOBCLOB set CLOBDATA = ? where ID = ?");
try {
clobInput.reset();
} catch (IOException ioe) {
fail("Failed to reset clob input stream: " + ioe.getMessage());
}
pStmt.setClob(1, clobInput, clobData.length());
pStmt.setInt(2, ID_SAMPLEVALUES);
assertEquals("Invalid update count", 1, pStmt.executeUpdate());
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where ID = " +
ID_SAMPLEVALUES);
rs.next();
Clob clob = rs.getClob(1);
rs.close();
stmt.close();
return clob;
}
static int skipLws(Reader input) throws IOException {
input.mark(1);
int c = input.read();
while (c == 32 || c == 9 || c == 10 || c == 13) {
input.mark(1);
c = input.read();
}
input.reset();
return c;
}
private static boolean trailingSpace(Reader in) throws IOException {
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
in.mark(9999);
while (true) {
int nextChar = in.read();
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
int followingChar = in.read();
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
in.reset();
return trailing;
}
}
private static boolean trailingSpace(Reader in) throws IOException {
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
in.mark(9999);
while (true) {
int nextChar = in.read();
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
int followingChar = in.read();
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
in.reset();
return trailing;
}
}
private static boolean trailingSpace(Reader in) throws IOException {
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
in.mark(9999);
while (true) {
int nextChar = in.read();
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
int followingChar = in.read();
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
in.reset();
return trailing;
}
}
private static boolean trailingSpace(Reader in) throws IOException {
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
in.mark(9999);
while (true) {
int nextChar = in.read();
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
int followingChar = in.read();
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
in.reset();
return trailing;
}
}
private static boolean trailingSpace(Reader in) throws IOException {
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
in.mark(9999);
while (true) {
int nextChar = in.read();
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
int followingChar = in.read();
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
in.reset();
return trailing;
}
}
@Override
public void reset() throws IOException {
Reader rdr = getReader();
if (rdr != null) {
rdr.reset();
}
}
private void handleEscaping(final Reader source,
final Position sourcePosition,
final StringSection currentSection,
final StringBuilder lookAhead) throws IOException, FinishedReadingException {
//if we get here, we just read in a back-slash from the source, now figure out what to do with it
int c = read(source, sourcePosition, lookAhead);
/*
The _only_ special escaping this template engine allows is to escape the sequences:
${ and <% and potential slashes in front of these. Escaping in any other sections of the
source string is ignored. The following is a source -> result mapping of a few values, assume a
binding of [alice: 'rabbit'].
Note: we don't do java escaping of slashes in the below
example, i.e. the source string is what you would see in a text editor when looking at your template
file:
source string result
'bob' -> 'bob'
'\bob' -> '\bob'
'\\bob' -> '\\bob'
'${alice}' -> 'rabbit'
'\${alice}' -> '${alice}'
'\\${alice}' -> '\rabbit'
'\\$bob' -> '\\$bob'
'\\' -> '\\'
'\\\' -> '\\\'
'%<= alice %>' -> 'rabbit'
'\%<= alice %>' -> '%<= alice %>'
*/
if (c == '\\') {
//this means we have received a double backslash sequence
//if this is followed by ${ or <% we output one backslash
//and interpret the following sequences with groovy, if followed by anything
//else we output the two backslashes and continue as usual
source.mark(3);
int d = read(source, sourcePosition, lookAhead);
c = read(source, sourcePosition, lookAhead);
clear(lookAhead);
if ((d == '$' && c == '{') ||
(d == '<' && c == '%')) {
source.reset();
currentSection.data.append('\\');
return;
} else {
currentSection.data.append('\\');
currentSection.data.append('\\');
currentSection.data.append((char) d);
}
} else if (c == '$') {
c = read(source, sourcePosition, lookAhead);
if (c == '{') {
currentSection.data.append('$');
} else {
currentSection.data.append('\\');
currentSection.data.append('$');
}
} else if (c == '<') {
c = read(source, sourcePosition, lookAhead);
if (c == '%') {
currentSection.data.append('<');
} else {
currentSection.data.append('\\');
currentSection.data.append('<');
}
} else {
currentSection.data.append('\\');
}
currentSection.data.append((char) c);
clear(lookAhead);
}
private static char peek(Reader reader) throws IOException {
reader.mark(1000);
final char c = (char) reader.read();
reader.reset();
return c;
}
/**
* LHEX can be read unambiguously with or without surrounding quotes so this
* parsing method for LHEX permits optional surrounding double quotes. Some
* buggy clients (libwww-perl for DIGEST auth) are known to send quoted LHEX
* when the specification requires just LHEX.
*
* <p>
* LHEX are, literally, lower-case hexadecimal digits. This implementation
* allows for upper-case digits as well, converting the returned value to
* lower-case.
*
* @return the sequence of LHEX (minus any surrounding quotes) if any was
* found, or <code>null</code> if data other LHEX was found
*/
static String readLhex(Reader input) throws IOException {
StringBuilder result = new StringBuilder();
boolean quoted = false;
skipLws(input);
input.mark(1);
int c = input.read();
if (c == '"') {
quoted = true;
} else if (c == -1 || !isHex(c)) {
return null;
} else {
if ('A' <= c && c <= 'F') {
c -= ('A' - 'a');
}
result.append((char) c);
}
input.mark(1);
c = input.read();
while (c != -1 && isHex(c)) {
if ('A' <= c && c <= 'F') {
c -= ('A' - 'a');
}
result.append((char) c);
input.mark(1);
c = input.read();
}
if (quoted) {
if (c != '"') {
return null;
}
} else {
// Use mark(1)/reset() rather than skip(-1) since skip() is a NOP
// once the end of the String has been reached.
input.reset();
}
if (c != -1 && result.length() == 0) {
return null;
} else {
return result.toString();
}
}
private static String readLineFromReaderWithMark(final Reader input)
throws IOException {
char[] cbuf = new char[charBufferSize];
try {
input.mark(charBufferSize);
} catch (IOException e) {
// this should never happen
LOG.warning("Caught exception setting mark on supporting reader: " + e);
// fallback
return readLineFromReaderWithoutMark(input);
}
// could be changed into do..while, but then
// we might create an additional StringBuilder
// instance at the end of the stream
int count = input.read(cbuf);
if (count == EOF) // we are at the end of the input data
return null;
StringBuilder line = new StringBuilder(expectedLineLength);
// now work on the buffer(s)
int ls = lineSeparatorIndex(cbuf, count);
while (ls == -1) {
line.append(cbuf, 0, count);
count = input.read(cbuf);
if (count == EOF) {
// we are at the end of the input data
return line.toString();
}
ls = lineSeparatorIndex(cbuf, count);
}
line.append(cbuf, 0, ls);
// correct ls if we have \r\n
int skipLS = 1;
if (ls + 1 < count) {
// we are not at the end of the buffer
if (cbuf[ls] == '\r' && cbuf[ls + 1] == '\n') {
skipLS++;
}
} else {
if (cbuf[ls] == '\r' && input.read() == '\n') {
skipLS++;
}
}
//reset() and skip over last linesep
input.reset();
input.skip(line.length() + skipLS);
return line.toString();
}
/**
* Read lines from a (buffered / mark-able) reader keeping all new-lines and line-feeds.
* @param reader the reader
* @return the line iterator
*/
protected static Iterator<CharSequence> readLines(final Reader reader) {
if (!reader.markSupported()) {
throw new IllegalArgumentException("mark support in reader required");
}
return new Iterator<CharSequence>() {
private CharSequence next = doNext();
private CharSequence doNext() {
StringBuffer strb = new StringBuffer(64); // CSOFF: MagicNumber
int c;
boolean eol = false;
try {
while ((c = reader.read()) >= 0) {
if (eol) {// && (c != '\n' && c != '\r')) {
reader.reset();
break;
}
if (c == '\n') {
eol = true;
}
strb.append((char) c);
reader.mark(1);
}
} catch (IOException xio) {
return null;
}
return strb.length() > 0 ? strb : null;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public CharSequence next() {
CharSequence current = next;
if (current != null) {
next = doNext();
}
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
};
}