java.io.InputStream.reset()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(296)

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

InputStream.reset介绍

[英]Resets this stream to the last marked location. Throws an IOException if the number of bytes read since the mark has been set is greater than the limit provided to mark, or if no mark has been set.

This implementation always throws an IOException and concrete subclasses should provide the proper implementation.
[中]将此流重置为最后标记的位置。如果自设置标记后读取的字节数大于为标记提供的限制,或者未设置标记,则引发IOException。
这个实现总是抛出IOException,具体的子类应该提供适当的实现。

代码示例

代码示例来源:origin: google/guava

@Override
 public synchronized void reset() throws IOException {
  if (!in.markSupported()) {
   throw new IOException("Mark not supported");
  }
  if (mark == -1) {
   throw new IOException("Mark not set");
  }

  in.reset();
  count = mark;
 }
}

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

private boolean hasMoreData(final InputStream in) throws IOException {
  in.mark(1);
  final int nextByte = in.read();
  in.reset();
  return nextByte >= 0;
}

代码示例来源:origin: spring-projects/spring-framework

public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
  this.headers = inputMessage.getHeaders();
  InputStream inputStream = inputMessage.getBody();
  if (inputStream.markSupported()) {
    inputStream.mark(1);
    this.body = (inputStream.read() != -1 ? inputStream : null);
    inputStream.reset();
  }
  else {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
    int b = pushbackInputStream.read();
    if (b == -1) {
      this.body = null;
    }
    else {
      this.body = pushbackInputStream;
      pushbackInputStream.unread(b);
    }
  }
}

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

fInputStream.mark(kBufSize);
byte[] inputBytes = new byte[kBufSize];   // Always make a new buffer because the
  bytesRead = IOUtils.readFully(fInputStream, inputBytes);
  if (bytesRead >= Integer.MAX_VALUE) {
    throw new IOException("Can't have read > Integer.MAX_VALUE bytes");
  fInputStream.reset();

代码示例来源:origin: stackoverflow.com

InputStream is = new BufferedInputStream(conn.getInputStream());
is.mark(is.available());
// Do the bound decoding
// inJustDecodeBounds =true
is.reset();  
// Do the actual decoding

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

if (! stream.markSupported()) {
  stream = new BufferedInputStream(stream);
    stream.reset();
    TikaInputStream tstream = TikaInputStream.get(stream, tmp);

代码示例来源:origin: bumptech/glide

/** Returns the ImageType for the given InputStream. */
@NonNull
public static ImageType getType(@NonNull List<ImageHeaderParser> parsers,
  @Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException {
 if (is == null) {
  return ImageType.UNKNOWN;
 }
 if (!is.markSupported()) {
  is = new RecyclableBufferedInputStream(is, byteArrayPool);
 }
 is.mark(MARK_POSITION);
 //noinspection ForLoopReplaceableByForEach to improve perf
 for (int i = 0, size = parsers.size(); i < size; i++) {
  ImageHeaderParser parser = parsers.get(i);
  try {
   ImageType type = parser.getType(is);
   if (type != ImageType.UNKNOWN) {
    return type;
   }
  } finally {
   is.reset();
  }
 }
 return ImageType.UNKNOWN;
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

protected InputStream resetStream(InputStream imageStream, ImageDecodingInfo decodingInfo) throws IOException {
  if (imageStream.markSupported()) {
    try {
      imageStream.reset();
      return imageStream;
    } catch (IOException ignored) {
    }
  }
  IoUtils.closeSilently(imageStream);
  return getImageStream(decodingInfo);
}

代码示例来源:origin: brianfrankcooper/YCSB

@Override
public void reset() {
 if (resetable) {
  try {
   ins.reset();
   ins.mark((int) len);
  } catch (IOException e) {
   throw new IllegalStateException("Failed to reset the input stream", e);
  }
 }
 throw new UnsupportedOperationException();
}

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

InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException {
  if (!stream.markSupported()) {
    stream = new BufferedInputStream(stream);
  }
  stream.mark(maxEntitySize + 1);
  final byte[] entity = new byte[maxEntitySize + 1];
  final int entitySize = stream.read(entity);
  b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset));
  if (entitySize > maxEntitySize) {
    b.append("...more...");
  }
  b.append('\n');
  stream.reset();
  return stream;
}

代码示例来源:origin: google/guava

@Override
public synchronized void reset() throws IOException {
 if (!in.markSupported()) {
  throw new IOException("Mark not supported");
 }
 if (mark == -1) {
  throw new IOException("Mark not set");
 }
 in.reset();
 left = mark;
}

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

@Override
public boolean isNext() throws IOException {
  bufferedIn.mark(1);
  final int nextByte = bufferedIn.read();
  bufferedIn.reset();
  return (nextByte > -1);
}

代码示例来源:origin: i2p/i2p.i2p

if (in instanceof BufferedInputStream) {
  in.mark(LOOKAHEAD_SIZE);
  long timeout = socket.getReadTimeout();
  socket.setReadTimeout(HASH_READ_TIMEOUT);
  in.reset();
} else {
  } else {
    throw new IOException("Peer wants another torrent (" + Base64.encode(peerInfoHash) 
               + ") while we only support (" + Base64.encode(coordinator.getInfoHash()) + ")");
  throw new IOException("Peer wants another torrent (" + Base64.encode(peerInfoHash) 
             + ") while we don't support that hash");

代码示例来源:origin: stackoverflow.com

InputStream data = new BufferedInputStream(realResponse.getEntity().getContent());
// data.markSupported() should return "true" now
data.mark(some_size);
// work with "data" now
...
data.reset();

代码示例来源:origin: bumptech/glide

/**
  * Returns the orientation for the given InputStream.
  */
 public static int getOrientation(@NonNull List<ImageHeaderParser> parsers,
   @Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException {
  if (is == null) {
   return ImageHeaderParser.UNKNOWN_ORIENTATION;
  }

  if (!is.markSupported()) {
   is = new RecyclableBufferedInputStream(is, byteArrayPool);
  }

  is.mark(MARK_POSITION);
  //noinspection ForLoopReplaceableByForEach to improve perf
  for (int i = 0, size = parsers.size(); i < size; i++) {
   ImageHeaderParser parser = parsers.get(i);
   try {
    int orientation = parser.getOrientation(is, byteArrayPool);
    if (orientation != ImageHeaderParser.UNKNOWN_ORIENTATION) {
     return orientation;
    }
   } finally {
    is.reset();
   }
  }

  return ImageHeaderParser.UNKNOWN_ORIENTATION;
 }
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Used to perform a last reset on the content input stream (if mark-supported); this is so
 * that, for backward compatibility reason, any "blind" retry (ie without calling reset) by
 * user of this library with the same input stream (such as ByteArrayInputStream) could
 * still succeed.
 *
 * @param t the failure
 * @return the failure as given
 */
private <T extends Throwable> T lastReset(final T t) {
  try {
    InputStream content = request.getContent();
    if (content != null) {
      if (content.markSupported()) {
        content.reset();
      }
    }
  } catch (Exception ex) {
    log.debug("FYI: failed to reset content inputstream before throwing up", ex);
  }
  return t;
}

代码示例来源:origin: google/guava

public void testLimit_skip() throws Exception {
 byte[] big = newPreFilledByteArray(5);
 InputStream bin = new ByteArrayInputStream(big);
 InputStream lin = ByteStreams.limit(bin, 2);
 // also test available
 lin.mark(2);
 assertEquals(2, lin.available());
 lin.skip(1);
 assertEquals(1, lin.available());
 lin.reset();
 assertEquals(2, lin.available());
 lin.skip(3);
 assertEquals(0, lin.available());
}

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

InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException {
  if (!stream.markSupported()) {
    stream = new BufferedInputStream(stream);
  }
  stream.mark(maxEntitySize + 1);
  final byte[] entity = new byte[maxEntitySize + 1];
  final int entitySize = stream.read(entity);
  b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset));
  if (entitySize > maxEntitySize) {
    b.append("...more...");
  }
  b.append('\n');
  stream.reset();
  return stream;
}

代码示例来源:origin: prestodb/presto

@Override
 public synchronized void reset() throws IOException {
  if (!in.markSupported()) {
   throw new IOException("Mark not supported");
  }
  if (mark == -1) {
   throw new IOException("Mark not set");
  }

  in.reset();
  count = mark;
 }
}

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

protected boolean isData(final InputStream in) throws IOException {
  in.mark(1);
  final int nextByte = in.read();
  in.reset();
  return nextByte > -1;
}

相关文章