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

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

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

InputStream.mark介绍

[英]Sets a mark position in this InputStream. The parameter readlimitindicates how many bytes can be read before the mark is invalidated. Sending reset() will reposition the stream back to the marked position provided readLimit has not been surpassed.

This default implementation does nothing and concrete subclasses must provide their own implementation.
[中]在此输入流中设置标记位置。参数ReadLimit指示在标记无效之前可以读取多少字节。发送reset()会将流重新定位回标记位置,前提是未超过readLimit。
这个默认实现不做任何事情,具体的子类必须提供自己的实现。

代码示例

代码示例来源:origin: skylot/jadx

public void mark(int len) throws IOException {
  if (!input.markSupported()) {
    throw new IOException("Mark not supported for input stream " + input.getClass());
  }
  input.mark(len);
}

代码示例来源: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/nifi

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

代码示例来源: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: frohoff/ysoserial

InputStream bufIn = is.markSupported() ? is : new BufferedInputStream(is);
bufIn.mark(4);
DataInputStream in = new DataInputStream(bufIn);
int magic = in.readInt();

代码示例来源: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: stackoverflow.com

InputStream is = null;
  try {
    is = new BufferedInputStream(new FileInputStream(new File(getCacheDir(), "mygif.gif")), 16 * 1024);
    is.mark(16 * 1024); 
  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
movie = Movie.decodeStream(is);

代码示例来源: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: apache/nifi

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

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

final InputStream bufferedInStream = new BufferedInputStream(fis);
final String serializationName;
try {
  bufferedInStream.mark(4096);
  final InputStream in = filename.endsWith(".gz") ? new GZIPInputStream(bufferedInStream) : bufferedInStream;
  final DataInputStream dis = new DataInputStream(in);
  serializationName = dis.readUTF();
  bufferedInStream.reset();
} catch (final EOFException eof) {
  fis.close();

代码示例来源: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: twitter/distributedlog

/**
 * Return an InputStream that reads from the provided InputStream, decompresses the data
 * and returns a new InputStream wrapping the underlying payload.
 *
 * Note that src is modified by this call.
 *
 * @return
 *      New Input stream with the underlying payload.
 * @throws Exception
 */
public static InputStream fromInputStream(InputStream src,
                     StatsLogger statsLogger) throws IOException {
  src.mark(VERSION_LENGTH);
  byte version = new DataInputStream(src).readByte();
  src.reset();
  EnvelopedEntry entry = new EnvelopedEntry(version, statsLogger);
  entry.readFully(new DataInputStream(src));
  return new ByteArrayInputStream(entry.getDecompressedPayload());
}

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

public InputStreamByteIterator(InputStream ins, long len) {
 this.len = len;
 this.ins = ins;
 off = 0;
 resetable = ins.markSupported();
 if (resetable) {
  ins.mark((int) len);
 }
}

代码示例来源: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: apache/nifi

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

代码示例来源: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: spotbugs/spotbugs

@SuppressFBWarnings("OS_OPEN_STREAM")
public static String getXMLType(@WillNotClose InputStream in) throws IOException {
  if (!in.markSupported()) {
    throw new IllegalArgumentException("Input stream does not support mark");
  }
  in.mark(5000);
  BufferedReader r = null;
  try {
    r = new BufferedReader(Util.getReader(in), 2000);
    String s;
    int count = 0;
    while (count < 4) {
      s = r.readLine();
      if (s == null) {
        break;
      }
      Matcher m = tag.matcher(s);
      if (m.find()) {
        return m.group(1);
      }
    }
    throw new IOException("Didn't find xml tag");
  } finally {
    in.reset();
  }
}

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

private static Bitmap decodeStream(InputStream is, BitmapFactory.Options options,
  DecodeCallbacks callbacks, BitmapPool bitmapPool) throws IOException {
 if (options.inJustDecodeBounds) {
  is.mark(MARK_POSITION);
 } else {
    is.reset();
    bitmapPool.put(options.inBitmap);
    options.inBitmap = null;
  is.reset();

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

/**
 * The readlimit parameter is ignored.
 */
@Override
public void mark(int readlimit) {
  abortIfNeeded();
  if ( !isAtStart )
    throw new UnsupportedOperationException("Chunk-encoded stream only supports mark() at the start of the stream.");
  if (is.markSupported()) {
    if (log.isDebugEnabled()) {
      log.debug("AwsChunkedEncodingInputStream marked at the start of the stream "
          + "(will directly mark the wrapped stream since it's mark-supported).");
    }
    is.mark(readlimit);
  }
  else {
    if (log.isDebugEnabled()) {
      log.debug("AwsChunkedEncodingInputStream marked at the start of the stream "
          + "(initializing the buffer since the wrapped stream is not mark-supported).");
    }
    decodedStreamBuffer = new DecodedStreamBuffer(maxBufferSize);
  }
}

相关文章