Skipping bytes

The skip() method reads a specified number of bytes and throws them away.

public int skip(long n) throws IOException

You might use this, for example, if you want to quickly move past a standard header or prefix to some data. For example, in the following code fragment skip() is used to move past some padding included in Java byte code to make sure that a jump table entry for a switch statement is aligned on a four byte boundary:

      case 171: // lookupswitch     
         pad = 3 - (position % 4);
         dis.skip(pad);
         defaultByte = dis.readInt();
         int npairs = dis.readInt();
         result = position + "    lookupswitch " + defaultByte + " " + npairs;
         for (int i = 0; i < npairs; i++) {
           int newPosition = position + pad + 12 + i*8;
           result += "\n" + newPosition + "    " 
            + dis.readInt() + " " + dis.readInt();
         }
(The complete program is included in Chapter 5 of my book, Java Secrets, IDG Books, 1997.)


Previous | Next | Top
Last Modified August 15, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu