/** * 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)); }
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; }
/** * 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); } } }
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); } } }
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);}
@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; }
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); } } }
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(); }
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();
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);
@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(); }
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();
@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; }