Code example for DataInputStream

Methods: readInt, readUnsignedShort

0
     * @exception IOException if there is a problem reading from the given 
     *      stream. 
     * @exception ClassFormatError if the class cannot be parsed correctly 
     */ 
    public void read(InputStream stream) throws IOException, ClassFormatError {
        DataInputStream classStream = new DataInputStream(stream);
 
        if (classStream.readInt() != CLASS_MAGIC) {
            throw new ClassFormatError("No Magic Code Found "
                + "- probably not a Java class file."); 
        } 
 
        // right we have a good looking class file. 
        /* int minorVersion = */ classStream.readUnsignedShort();
        /* int majorVersion = */ classStream.readUnsignedShort();
 
        // read the constant pool in and resolve it 
        constantPool = new ConstantPool();
 
        constantPool.read(classStream);
        constantPool.resolve();
 
        /* int accessFlags = */ classStream.readUnsignedShort();
        int thisClassIndex = classStream.readUnsignedShort();
        /* int superClassIndex = */ classStream.readUnsignedShort();
        ClassCPInfo classInfo
            = (ClassCPInfo) constantPool.getEntry(thisClassIndex);
        className  = classInfo.getClassName();
    }