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

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

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

InputStream.markSupported介绍

[英]Indicates whether this stream supports the mark() and reset() methods. The default implementation returns false.
[中]指示此流是否支持mark()和reset()方法。默认实现返回false。

代码示例

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

/**
 * 将{@link InputStream}转换为支持mark标记的流<br>
 * 若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之
 * 
 * @param in 流
 * @return {@link InputStream}
 * @since 4.0.9
 */
public static InputStream toMarkSupportStream(InputStream in) {
  if (null == in) {
    return null;
  }
  if (false == in.markSupported()) {
    return new BufferedInputStream(in);
  }
  return in;
}

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

public void testReadSingleByte() throws Exception {
 ByteSource source = newByteSource(0, 10);
 ByteSource joined = ByteSource.concat(source, source);
 assertEquals(20, joined.size());
 InputStream in = joined.openStream();
 assertFalse(in.markSupported());
 assertEquals(10, in.available());
 int total = 0;
 while (in.read() != -1) {
  total++;
 }
 assertEquals(0, in.available());
 assertEquals(20, total);
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testResetNoMark() throws Exception {
  InputStream input = makeInputStream(makeOrderedArray(10));
  if (!input.markSupported()) {
    return; // Not supported, skip test
  }
  int read = input.read();
  assertEquals(0, read);
  // No mark may either throw exception, or reset to beginning of stream.
  try {
    input.reset();
    assertEquals("Re-read of reset data should be same", 0, input.read());
  }
  catch (Exception e) {
    assertTrue("Wrong no mark IOException message", e.getMessage().contains("mark"));
  }
}

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

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

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

/**
 * 将{@link InputStream}转换为支持mark标记的流<br>
 * 若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之
 * 
 * @param in 流
 * @return {@link InputStream}
 * @since 4.0.9
 */
public static InputStream toMarkSupportStream(InputStream in) {
  if (null == in) {
    return null;
  }
  if (false == in.markSupported()) {
    return new BufferedInputStream(in);
  }
  return in;
}

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

private void validateInputStreamReads(InputStream test, InputStream check)
  throws IOException {
 byte[] bt = new byte[7];
 byte[] bc = new byte[7];
 while (true) {
  int t = test.read();
  int c = check.read();
  Assert.assertEquals(c, t);
  if (-1 == t) break;
  t = test.read(bt);
  c = check.read(bc);
  Assert.assertEquals(c, t);
  Assert.assertArrayEquals(bt, bc);
  if (-1 == t) break;
  t = test.read(bt, 1, 4);
  c = check.read(bc, 1, 4);
  Assert.assertEquals(c, t);
  Assert.assertArrayEquals(bt, bc);
  if (-1 == t) break;
 }
 Assert.assertEquals(0, test.skip(5));
 Assert.assertEquals(0, test.available());
 Assert.assertFalse(test.getClass() != ByteArrayInputStream.class && test.markSupported());
 test.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: 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: 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: org.apache.poi/poi

/**
   * Checks if an {@link InputStream} can be reset (i.e. used for checking the header magic) and wraps it if not
   *
   * @param stream stream to be checked for wrapping
   * @return a mark enabled stream
   */
  public static InputStream prepareToCheckMagic(InputStream stream) {
    if (stream.markSupported()) {
      return stream;
    }
    // we used to process the data via a PushbackInputStream, but user code could provide a too small one
    // so we use a BufferedInputStream instead now
    return new BufferedInputStream(stream);
  }
}

代码示例来源: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: 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: 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);
  }
}

相关文章