Java - ByteArrayInputStream skip()



Description

The Java ByteArrayInputStream skip(long n) method skips n bytes from the input stream.

Declaration

Following is the declaration for java.io.ByteArrayInputStream.skip(long n) method −

public long skip(long n)

Parameters

n − The count of bytes to be skipped

Return Value

This method returns the actual number of bytes skipped.

Exception

NA

Example 1

The following example shows the usage of Java ByteArrayInputStream skip() method to skip 1 byte while iterating through the data stream. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We're reading bytes using read() method in a while loop and skip 1 byte during iteration and printed the value.

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         int value = 0;
         
         // read till the end of the stream
         while((value = bais.read())!=-1) {
            
            // skip single byte
            bais.skip(1);
            System.out.println(value);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

65
67
69

Example 2

The following example shows the usage of Java ByteArrayInputStream skip() method to skip multiple bytes while iterating through the data stream. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We're reading bytes using read() method in a while loop and skip 2 bytes during iteration and printed the value.

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         int value = 0;
         
         // read till the end of the stream
         while((value = bais.read())!=-1) {
            
            // skip multiple bytes
            bais.skip(2);
            System.out.println(value);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

65
68
java_bytearrayinputstream.htm
Advertisements