下面列出了java.io.Reader#markSupported ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public SortedJSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
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;
}
}
/**
* Construct a JSONTokener from a Reader. The caller must close the Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.characterPreviousLine = 0;
this.line = 1;
}
/**
* Construct a JSONTokener from a reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported() ?
reader : new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported() ?
reader : new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader
* A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported() ? reader : new BufferedReader(
reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
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;
}
}
/**
* Construct a JSONTokener from a string.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported() ?
reader : new BufferedReader(reader);
this.useLastChar = false;
this.index = 0;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
public JSONTokenizer(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported() ?
reader : new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
}
/**
* 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.");
}
};
}
/**
* Read a single, whole line from the given Reader. This method is designed for use with
* Readers that support the {@code mark()} operation like BufferReader. It has a fallback
* behavior for Readers that don't support mark() but the behavior doesn't correctly
* detect multi-character line termination (e.g. carriage return followed by linefeed).
* We recommend for Readers that don't support mark() you consider using one of the
* following methods instead: eachLine, readLines, or iterator.
*
* @param self a Reader
* @return a line
* @throws IOException if an IOException occurs.
* @see #readLines(java.io.Reader)
* @see #iterator(java.io.Reader)
* @see #eachLine(java.io.Reader, groovy.lang.Closure)
* @since 1.0
*/
public static String readLine(Reader self) throws IOException {
if (self instanceof BufferedReader) {
BufferedReader br = (BufferedReader) self;
return br.readLine();
}
if (self.markSupported()) {
return readLineFromReaderWithMark(self);
}
return readLineFromReaderWithoutMark(self);
}