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

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

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

InputStream.skip介绍

[英]Skips at most n bytes in this stream. This method does nothing and returns 0 if n is negative, but some subclasses may throw.

Note the "at most" in the description of this method: this method may choose to skip fewer bytes than requested. Callers should always check the return value.

This default implementation reads bytes into a temporary buffer. Concrete subclasses should provide their own implementation.
[中]此流中最多跳过n个字节。此方法不执行任何操作,如果n为负,则返回0,但某些子类可能会抛出。
请注意此方法描述中的“最多”:此方法可能选择跳过的字节数少于请求的字节数。调用方应始终检查返回值。
此默认实现将字节读入临时缓冲区。具体的子类应该提供自己的实现。

代码示例

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

/**
 * Attempts to skip up to {@code n} bytes from the given input stream, but not more than {@code
 * in.available()} bytes. This prevents {@code FileInputStream} from skipping more bytes than
 * actually remain in the file, something that it {@linkplain java.io.FileInputStream#skip(long)
 * specifies} it can do in its Javadoc despite the fact that it is violating the contract of
 * {@code InputStream.skip()}.
 */
private static long skipSafely(InputStream in, long n) throws IOException {
 int available = in.available();
 return available == 0 ? 0 : in.skip(Math.min(available, n));
}

代码示例来源:origin: Tencent/tinker

public static void skipAll(InputStream in) throws IOException {
  do {
    in.skip(Long.MAX_VALUE);
  } while (in.read() != -1);
}
/**

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

public static int keyPress(String msg) {
  System.out.println(msg);
  try {
    int ret=System.in.read();
    System.in.skip(System.in.available());
    return ret;
  }
  catch(IOException e) {
    return -1;
  }
}

代码示例来源:origin: naman14/Timber

private static int readOgg(byte[] buf, InputStream in, int bytesinpage, int skip) throws IOException {
  int toread = skip!=-1?skip:buf.length;
  int offset = 0;
  while(toread>0){
    if(bytesinpage==0){
      byte magic[] = new byte[4];
      in.read(magic);
      if(!Arrays.equals(magic,new byte[]{'O','g','g','S'})){
        in.close();
        throw new IOException();
      }
      byte header[] = new byte[23];
      in.read(header);
      int count = header[22]& 0xFF;
      while(count-->0){
        bytesinpage += in.read();
      }
    }
    int read = toread;
    if(bytesinpage-toread<0)read = bytesinpage;
    if(skip != -1)
      in.skip(read);
    else
      in.read(buf, offset, read);
    offset += read;
    toread -= read;
    bytesinpage -= read;
  }
  return bytesinpage;
}

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

/**
  * Skips any unread bytes from InputStream and closes it.
  */
 static void skipAndCloseQuietly(InputStream stream) {
  try {
   if (stream instanceof KnownLength && stream.available() > 0) {
    stream.skip(stream.available());
   } else {
    //don't expect this for an inputStream provided by gRPC but just to be on safe side.
    byte[] skipBuffer = new byte[4096];
    while (true) {
     int read = stream.read(skipBuffer);
     if (read < skipBuffer.length) {
      break;
     }
    }
   }
   stream.close();
  } catch (Exception e) {
   log.log(Level.WARNING, "failed to skip/close the input stream, may cause memory leak", e);
  }
 }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public void setTime(float time) {
    if (time != 0f) {
      throw new UnsupportedOperationException("Seeking WAV files not supported");
    }
    InputStream newStream = info.openStream();
    try {
      newStream.skip(resetOffset);
      this.in = new BufferedInputStream(newStream);
    } catch (IOException ex) {
      // Resource could have gotten lost, etc.
      try {
        newStream.close();
      } catch (IOException ex2) {
      }
      throw new RuntimeException(ex);
    }
  }
}

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

long end = range[1];
inputStream = new BufferedInputStream(new FileInputStream(file));
if (inputStream.skip(start) != start)
    throw new RuntimeException("File skip error");
outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
long position = start;
for (int len; position <= end && (len = inputStream.read(buffer)) != -1;) {
  if (position + len <= end) {
    outputStream.write(buffer, 0, len);
  try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}

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

@Override
public long skip(long total) throws IOException {
 if (total < 0) {
  return 0;
 }
 long toSkip = total;
 while (toSkip > 0) {
  long skipped = is.skip(toSkip);
  if (skipped > 0) {
   toSkip -= skipped;
  } else {
   // Skip has no specific contract as to what happens when you reach the end of
   // the stream. To differentiate between temporarily not having more data and
   // having finished the stream, we read a single byte when we fail to skip any
   // amount of data.
   int testEofByte = is.read();
   if (testEofByte == -1) {
    break;
   } else {
    toSkip--;
   }
  }
 }
 return total - toSkip;
}

代码示例来源:origin: hierynomus/sshj

private void checkAndFlushProxyResponse()throws IOException {
    InputStream socketInput = getInputStream();
    byte[] tmpBuffer = new byte[512];
    int len = socketInput.read(tmpBuffer, 0, tmpBuffer.length);

    if (len == 0) {
      throw new SocketException("Empty response from proxy");
    }

    String proxyResponse = new String(tmpBuffer, 0, len, IOUtils.UTF8);

    // Expecting HTTP/1.x 200 OK
    if (proxyResponse.contains("200")) {
      // Flush any outstanding message in buffer
      if (socketInput.available() > 0) {
        socketInput.skip(socketInput.available());
      }
      // Proxy Connect Successful
    } else {
      throw new SocketException("Fail to create Socket\nResponse was:" + proxyResponse);
    }
  }
}

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

in.skip(BSUtil.HEADER_SIZE);
DataInputStream ctrlBlockIn = new DataInputStream(new GZIPInputStream(in));
in.skip(ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream diffBlockIn = new GZIPInputStream(in);
in.skip(diffBlockLen + ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream extraBlockIn = new GZIPInputStream(in);
diffBlockIn.close();
extraBlockIn.close();

代码示例来源:origin: apache/incubator-dubbo

public static void skipUnusedStream(InputStream is) throws IOException {
    if (is.available() > 0) {
      is.skip(is.available());
    }
  }
}

代码示例来源:origin: cmusphinx/sphinx4

try {
  InputStream dataStream = new FileInputStream(file);
  dataStream.skip(startOffset * bytesPerFrame);
  if (dataStream.read(data) != data.length) {
    dataStream.close();
    throw new CTLException("Unable to read " + data.length + " bytes of utterance " + name);
  dataStream.close();

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

public static void skipAll(InputStream in) throws IOException {
  do {
    in.skip(Long.MAX_VALUE);
  } while (in.read() != -1);
}

代码示例来源:origin: sannies/mp4parser

private void find(InputStream fis) throws IOException {
  while (fis.available() > 0) {
    byte[] header = new byte[8];
    fis.read(header);
      if (type.equals("tkhd")) {
        lastTkhd = new byte[(int) (size - 8)];
        fis.read(lastTkhd);
      } else {
        if (type.equals("hdlr")) {
          byte[] hdlr = new byte[(int) (size - 8)];
          fis.read(hdlr);
          if (hdlr[8] == 0x76 && hdlr[9] == 0x69 && hdlr[10] == 0x64 && hdlr[11] == 0x65) {
            System.out.println("Video Track Header identified");
          fis.skip(size - 8);

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

@Test
public void testSkip() throws IOException {
  byte[] src = "HELLO MINA!".getBytes();
  ByteBuffer bb = ByteBuffer.wrap(src);
  InputStream is = new ByteBufferInputStream(bb);
  is.skip(6);
  assertEquals(5, is.available());
  assertEquals('M', is.read());
  assertEquals('I', is.read());
  assertEquals('N', is.read());
  assertEquals('A', is.read());
  is.skip((long) Integer.MAX_VALUE + 1);
  assertEquals(-1, is.read());
  is.close();
}

代码示例来源:origin: Tencent/tinker

in.skip(BSUtil.HEADER_SIZE);
DataInputStream ctrlBlockIn = new DataInputStream(new GZIPInputStream(in));
in.skip(ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream diffBlockIn = new GZIPInputStream(in);
in.skip(diffBlockLen + ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream extraBlockIn = new GZIPInputStream(in);
  diffBlockIn.close();
  extraBlockIn.close();
} finally {
  oldFile.close();

代码示例来源:origin: apache/incubator-dubbo

public static void skipUnusedStream(InputStream is) throws IOException {
    if (is.available() > 0) {
      is.skip(is.available());
    }
  }
}

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

@Test
public void testSkipWithoutBOM() throws Exception {
  final byte[] data = new byte[] { 'A', 'B', 'C', 'D' };
  final InputStream in = new BOMInputStream(createUtf8DataStream(data, false));
  in.skip(2L);
  assertEquals('C', in.read());
  in.close();
}

代码示例来源:origin: Yalantis/uCrop

@Override
public long skip(long total) throws IOException {
  if (total < 0) {
    return 0;
  }
  long toSkip = total;
  while (toSkip > 0) {
    long skipped = is.skip(toSkip);
    if (skipped > 0) {
      toSkip -= skipped;
    } else {
      // Skip has no specific contract as to what happens when you reach the end of
      // the stream. To differentiate between temporarily not having more data and
      // having finished the stream, we read a single byte when we fail to skip any
      // amount of data.
      int testEofByte = is.read();
      if (testEofByte == -1) {
        break;
      } else {
        toSkip--;
      }
    }
  }
  return total - toSkip;
}

相关文章