java.io.Reader.mark()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(134)

本文整理了Java中java.io.Reader.mark方法的一些代码示例,展示了Reader.mark的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reader.mark方法的具体详情如下:
包路径:java.io.Reader
类名称:Reader
方法名:mark

Reader.mark介绍

[英]Sets a mark position in this reader. The parameter readLimitindicates how many characters can be read before the mark is invalidated. Calling reset() will reposition the reader back to the marked position if readLimit has not been surpassed.

This default implementation simply throws an IOException; subclasses must provide their own implementation.
[中]设置此读卡器中的标记位置。参数ReadLimit指示在标记无效之前可以读取的字符数。如果未超过readLimit,调用reset()会将读卡器重新定位到标记位置。
这个默认实现只是抛出一个IOException;子类必须提供自己的实现。

代码示例

代码示例来源:origin: jersey/jersey

private static Reader ensureNonEmptyReader(Reader reader) throws XMLStreamException {
  try {
    Reader mr = reader.markSupported() ? reader : new BufferedReader(reader);
    mr.mark(1);
    if (mr.read() == -1) {
      throw new XMLStreamException("JSON expression can not be empty!");
    }
    mr.reset();
    return mr;
  } catch (IOException ex) {
    throw new XMLStreamException(ex);
  }
}

代码示例来源:origin: pmd/pmd

public static Reader skipBOM(Reader source) {
  Reader in = new BufferedReader(source);
  try {
    in.mark(1);
    int firstCharacter = in.read();
    if (firstCharacter != '\ufeff') {
      in.reset();
    }
  } catch (IOException e) {
    throw new RuntimeException("Error while trying to skip BOM marker", e);
  }
  return in;
}

代码示例来源:origin: com.h2database/h2

private void initRead() throws IOException {
  if (input == null) {
    try {
      InputStream in = FileUtils.newInputStream(fileName);
      in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE);
      input = new InputStreamReader(in, characterSet);
    } catch (IOException e) {
      close();
      throw e;
    }
  }
  if (!input.markSupported()) {
    input = new BufferedReader(input);
  }
  input.mark(1);
  int bom = input.read();
  if (bom != 0xfeff) {
    // Microsoft Excel compatibility
    // ignore pseudo-BOM
    input.reset();
  }
  inputBuffer = new char[Constants.IO_BUFFER_SIZE * 2];
  if (columnNames == null) {
    readHeader();
  }
}

代码示例来源:origin: org.jsoup/jsoup

private void bufferUp() {
  if (bufPos < bufSplitPoint)
    return;
  try {
    reader.skip(bufPos);
    reader.mark(maxBufferLen);
    final int read = reader.read(charBuf);
    reader.reset();
    if (read != -1) {
      bufLength = read;
      readerPos += bufPos;
      bufPos = 0;
      bufMark = 0;
      bufSplitPoint = bufLength > readAheadLimit ? readAheadLimit : bufLength;
    }
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}

代码示例来源:origin: lealone/Lealone

private void initRead() throws IOException {
  if (input == null) {
    try {
      InputStream in = FileUtils.newInputStream(fileName);
      in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE);
      input = new InputStreamReader(in, characterSet);
    } catch (IOException e) {
      close();
      throw e;
    }
  }
  if (!input.markSupported()) {
    input = new BufferedReader(input);
  }
  input.mark(1);
  int bom = input.read();
  if (bom != 0xfeff) {
    // Microsoft Excel compatibility
    // ignore pseudo-BOM
    input.reset();
  }
  inputBuffer = new char[Constants.IO_BUFFER_SIZE * 2];
  if (columnNames == null) {
    readHeader();
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Determine if the source string still contains characters that next()
 * can consume.
 * @return true if not yet at the end of the source.
 * @throws JSONException thrown if there is an error stepping forward
 *  or backward while checking for more data.
 */
public boolean more() throws JSONException {
  if(this.usePrevious) {
    return true;
  }
  try {
    this.reader.mark(1);
  } catch (IOException e) {
    throw new JSONException("Unable to preserve stream position", e);
  }
  try {
    // -1 is EOF, but next() can not consume the null character '\0'
    if(this.reader.read() <= 0) {
      this.eof = true;
      return false;
    }
    this.reader.reset();
  } catch (IOException e) {
    throw new JSONException("Unable to read the next character from the stream", e);
  }
  return true;
}

代码示例来源:origin: groovy/groovy-core

while ((c = reader.read()) != -1) {
  if (c == '<') {
    reader.mark(1);
    c = reader.read();
    if (c != '%') {
      sw.write('<');
      reader.reset();
    } else {
      reader.mark(1);
      c = reader.read();
      if (c == '=') {
        groovyExpression(reader, sw);
      } else {
        reader.reset();
        groovySection(reader, sw);
    reader.mark(1);
    c = reader.read();
    if (c != '{') {
      sw.write('$');
      reader.reset();
    } else {
      reader.mark(1);
      sw.write("${");
      processGSstring(reader, sw);
      reader.mark(1);
      c = reader.read();
      if (c != '\n') {

代码示例来源:origin: loklak/loklak_server

long startCharacter = this.character;
  long startLine = this.line;
  this.reader.mark(1000000);
  do {
    c = this.next();
      this.reader.reset();
      this.index = startIndex;
      this.character = startCharacter;
  this.reader.mark(1);
} catch (IOException exception) {
  throw new JSONException(exception);

代码示例来源:origin: commons-io/commons-io

@Test
public void testMarkNotSupported() throws Exception {
  final Reader reader = new TestNullReader(100, false, true);
  assertFalse("Mark Should NOT be Supported", reader.markSupported());
  try {
    reader.mark(5);
    fail("mark() should throw UnsupportedOperationException");
  } catch (final UnsupportedOperationException e) {
    assertEquals("mark() error message",  "Mark not supported", e.getMessage());
  }
  try {
    reader.reset();
    fail("reset() should throw UnsupportedOperationException");
  } catch (final UnsupportedOperationException e) {
    assertEquals("reset() error message",  "Mark not supported", e.getMessage());
  }
  reader.close();
}

代码示例来源:origin: org.codehaus.groovy/groovy

char[] cbuf = new char[charBufferSize];
try {
  input.mark(charBufferSize);
} catch (IOException e) {
input.reset();
input.skip(line.length() + skipLS);
return line.toString();

代码示例来源:origin: commons-io/commons-io

reader.reset();
  fail("Read limit exceeded, expected IOException ");
} catch (final IOException e) {
reader.mark(readlimit);
reader.reset();
  reader.reset();
  fail("Read limit exceeded, expected IOException ");
} catch (final IOException e) {

代码示例来源:origin: Activiti/Activiti

/**
 * Skip characters until the next character is the requested character. If the requested character is not found, no characters are skipped.
 * 
 * @param to
 *          A character to skip to.
 * @return The requested character, or zero if the requested character is not found.
 */
public char skipTo(char to) throws JSONException {
 char c;
 try {
  int startIndex = this.index;
  int startCharacter = this.character;
  int startLine = this.line;
  reader.mark(Integer.MAX_VALUE);
  do {
   c = next();
   if (c == 0) {
    reader.reset();
    this.index = startIndex;
    this.character = startCharacter;
    this.line = startLine;
    return c;
   }
  } while (c != to);
 } catch (IOException exc) {
  throw new JSONException(exc);
 }
 back();
 return c;
}

代码示例来源:origin: apache/hive

@Test
public void testMarkReset() throws IOException {
 Reader reader = new ReusableStringReader();
 if (reader.markSupported()) {
  ((ReusableStringReader)reader).set(fox);
  assertTrue(reader.ready());
  
  char[] cc = new char[6];
  int read;
  read = reader.read(cc);
  assertEquals(6, read);
  assertEquals("Quick ", new String(cc));
  
  reader.mark(100);
  
  read = reader.read(cc);
  assertEquals(6, read);
  assertEquals("brown ", new String(cc));
  
  reader.reset();
  read = reader.read(cc);
  assertEquals(6, read);
  assertEquals("brown ", new String(cc));
 }
 reader.close();
}

代码示例来源:origin: looly/hutool

/**
 * Skip characters until the next character is the requested character. If the requested character is not found, no characters are skipped. 在遇到指定字符前,跳过其它字符。如果字符未找到,则不跳过任何字符。
 * 
 * @param to 需要定位的字符
 * @return 定位的字符,如果字符未找到返回0
 */
public char skipTo(char to) throws JSONException {
  char c;
  try {
    long startIndex = this.index;
    long startCharacter = this.character;
    long startLine = this.line;
    this.reader.mark(1000000);
    do {
      c = this.next();
      if (c == 0) {
        this.reader.reset();
        this.index = startIndex;
        this.character = startCharacter;
        this.line = startLine;
        return c;
      }
    } while (c != to);
  } catch (IOException exception) {
    throw new JSONException(exception);
  }
  this.back();
  return c;
}

代码示例来源:origin: spullara/mustache.java

br.mark(1);
if (sm.length() == 1 || br.read() == sm.charAt(1)) {
 br.mark(1);
 c = br.read();
 boolean delimiter = c == '=';
  sb.append((char) c);
 } else {
  br.reset();
  br.mark(1);
  if (delimiter) {
   if (c == '=') {
    } else {
     br.reset();
} else {
 br.reset();

代码示例来源:origin: looly/hutool

/**
 * Skip characters until the next character is the requested character. If the requested character is not found, no characters are skipped. 在遇到指定字符前,跳过其它字符。如果字符未找到,则不跳过任何字符。
 * 
 * @param to 需要定位的字符
 * @return 定位的字符,如果字符未找到返回0
 */
public char skipTo(char to) throws JSONException {
  char c;
  try {
    long startIndex = this.index;
    long startCharacter = this.character;
    long startLine = this.line;
    this.reader.mark(1000000);
    do {
      c = this.next();
      if (c == 0) {
        this.reader.reset();
        this.index = startIndex;
        this.character = startCharacter;
        this.line = startLine;
        return c;
      }
    } while (c != to);
  } catch (IOException exception) {
    throw new JSONException(exception);
  }
  this.back();
  return c;
}

代码示例来源:origin: org.apache.commons/commons-lang3

reader = sb.asReader();
assertEquals('s', reader.read());
reader.mark(-1);
char[] array = new char[3];
assertEquals(3, reader.read(array, 0, 3));
assertEquals('m', array[1]);
assertEquals('e', array[2]);
reader.reset();
assertEquals(1, reader.read(array, 1, 1));
assertEquals('o', array[0]);
assertEquals(-1, reader.read(array, 0, 1));
reader.reset();
array = new char[30];
assertEquals(9, reader.read(array, 0, 30));

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Skip characters until the next character is the requested character.
 * If the requested character is not found, no characters are skipped.
 * @param to A character to skip to.
 * @return The requested character, or zero if the requested character
 * is not found.
 */
public char skipTo(char to) throws JSONException {
  char c;
  try {
    long startIndex = this.index;
    long startCharacter = this.character;
    long startLine = this.line;
    this.reader.mark(1000000);
    do {
      c = this.next();
      if (c == 0) {
        this.reader.reset();
        this.index = startIndex;
        this.character = startCharacter;
        this.line = startLine;
        return c;
      }
    } while (c != to);
  } catch (IOException exception) {
    throw new JSONException(exception);
  }
  this.back();
  return c;
}

代码示例来源:origin: com.sun.jersey/jersey-json

private static Reader ensureNonEmptyReader(Reader reader) throws XMLStreamException {
  try {
    Reader mr = reader.markSupported() ? reader : new BufferedReader(reader);
    mr.mark(1);
    if (mr.read() == -1) {
      throw new XMLStreamException("JSON expression can not be empty!");
    }
    mr.reset();
    return mr;
  } catch (IOException ex) {
    throw new XMLStreamException(ex);
  }
}

代码示例来源:origin: alibaba/mdrill

int startCharacter = this.character;
int startLine = this.line;
reader.mark(Integer.MAX_VALUE);
do {
  c = next();
  if (c == 0) {
    reader.reset();
    this.index = startIndex;
    this.character = startCharacter;

相关文章

微信公众号

最新文章

更多