Java - ByteArrayInputStream skip(long n) method



Description

The Java ByteArrayInputStream skip(long n) method is used to skip over and discard n bytes of data from the input stream. The method returns the actual number of bytes skipped, which may be less than n if the end of the stream is reached.

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 - Using ByteArrayInputStream skip() method

The following example shows the usage of Java ByteArrayInputStream skip(long n) 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.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamDemo {
   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 - Using ByteArrayInputStream skip(long n) method

The following example shows the usage of Java ByteArrayInputStream skip(long n) 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.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamDemo {
   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

Example - Using ByteArrayInputStream skip(long n) method

The following example shows the usage of Java ByteArrayInputStream skip(long n) method.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array as the data source
      byte[] data = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}; // "Hello World"

      // Create a ByteArrayInputStream
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      try {
         // Read and print the first two bytes
         System.out.print("Initial bytes read: ");
         System.out.print((char) inputStream.read()); // Read 'H'
         System.out.print((char) inputStream.read()); // Read 'e'
         System.out.println();

         // Skip the next 3 bytes
         long bytesSkipped = inputStream.skip(3);
         System.out.println("Bytes skipped: " + bytesSkipped);

         // Read and print the next byte after skipping
         System.out.print("Next byte after skipping: ");
         System.out.println((char) inputStream.read()); 

         // Skip more bytes than remaining in the stream
         bytesSkipped = inputStream.skip(10); // Attempt to skip 10 bytes
         System.out.println("Bytes skipped beyond remaining data: " + bytesSkipped);

         // Attempt to read after skipping
         int nextByte = inputStream.read();
         if (nextByte == -1) {
            System.out.println("End of stream reached.");
         } else {
            System.out.println("Next byte read: " + (char) nextByte);
         }
      } catch (Exception e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

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

Initial bytes read: He
Bytes skipped: 3
Next byte after skipping: 
Bytes skipped beyond remaining data: 5
End of stream reached.

Explanation

  • Initialization

    • A ByteArrayInputStream is created using a byte array that contains the ASCII values for "Hello World".

  • Reading Initial Bytes

    • The first two bytes ('H' and 'e') are read and printed.

  • Skipping Bytes

    • The skip(3) method is called to skip the next 3 bytes ('l', 'l', and 'o').

    • The method returns the number of bytes actually skipped, which is printed.

  • Reading After Skipping

    • The next byte is read after the skipped portion, which is ''.

  • Attempting to Skip Beyond End

    • Another skip(10) call is made, but since fewer bytes remain in the stream, only the remaining bytes are skipped.

    • The method handles this gracefully and returns the actual number of bytes skipped.

  • Handling End of Stream

    • After skipping past the remaining bytes, an attempt to read results in -1, indicating the end of the stream.

Key Points

  • Usage

    • The skip(long n) method is useful when you want to ignore certain parts of the input data.

  • Return Value

    • The method returns the number of bytes actually skipped, which may be less than n if the end of the stream is reached.

  • Graceful Handling

    • If you attempt to skip beyond the available bytes, it will only skip the remaining bytes without throwing an error.

  • End of Stream

    • After skipping past all data, any subsequent read() calls will return -1.

This method is particularly helpful when processing input streams with sections of data that can be ignored.

java_bytearrayinputstream.htm
Advertisements