private void readHeader(final PushbackInputStream stream)
            throws IOException {
        UnbufferedASCIIReader hdrReader = new UnbufferedASCIIReader(stream);
        String magic = hdrReader.readLine();
        if (!"ply".equals(magic)) {
            throw new IOException("Invalid PLY file: does not start "
                    + "with 'ply'.");
        }

        format = null;
        ElementType.HeaderEntry currentElement = null;
        List<Property> currentElementProperties = null;
        elements = new ArrayList<ElementType>();
        elementCounts = new HashMap<String, Integer>();
        rawHeaders = new ArrayList<String>();

        for (String line = hdrReader.readLine(); true;
                line = hdrReader.readLine()) {
            if (line == null) {
                throw new IOException("Unexpected end of file while "
                        + "reading the header.");
            }
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            rawHeaders.add(line);
            if (line.startsWith("format ")) {
                if (format != null) {
                    throw new IOException("Multiple format definitions.");
                }
                format = Format.parse(line);
            } else if (line.startsWith("element ")) {
                if (currentElement != null) {
                    // finish the last element
                    ElementType element = new ElementType(
                            currentElement.getName(),
                            currentElementProperties);
                    elements.add(element);
                    elementCounts.put(currentElement.getName(),
                            currentElement.getCount());
                    currentElement = null;
                    currentElementProperties = null;
                }
                currentElement = ElementType.parse(line);
                currentElementProperties = new ArrayList<Property>();
            } else if (line.startsWith("property ")) {
                if (currentElement == null) {
                    throw new IOException("Property without element found.");
                }
                Property property = Property.parse(line);
                currentElementProperties.add(property);
            } else if (line.startsWith("end_header")) {
                break;
            }
        }

        if (currentElement != null) {
            // finish the last element
            ElementType element = new ElementType(currentElement.getName(),
                    currentElementProperties);
            elements.add(element);
            elementCounts.put(currentElement.getName(),
                    currentElement.getCount());
            currentElement = null;
            currentElementProperties = null;
        }
        elements = Collections.unmodifiableList(elements);

        if (format == null) {
            throw new IOException("Missing format header entry.");
        }

        switch (format) {
            case ASCII:
                this.asciiReader = new BufferedReader(new InputStreamReader(
                        stream, Charset.forName("US-ASCII")));
                this.binaryStream = null;
                break;
            case BINARY_BIG_ENDIAN:
                this.asciiReader = null;
                this.binaryStream = new BinaryPlyInputStream(
                        Channels.newChannel(stream), ByteOrder.BIG_ENDIAN);
                break;
            case BINARY_LITTLE_ENDIAN:
                this.asciiReader = null;
                this.binaryStream = new BinaryPlyInputStream(
                        Channels.newChannel(stream), ByteOrder.LITTLE_ENDIAN);
                break;
            default:
                throw new IOException("Unsupported format: " + format);
        }
    }
private void readHeader ( final PushbackInputStream stream ) throws IOException { UnbufferedASCIIReader hdrReader = new UnbufferedASCIIReader ( stream ) ; String magic = hdrReader . readLine ( ) ; if ( ! "ply" . equals ( magic ) ) { throw new IOException ( "Invalid PLY file: does not start " + "with 'ply'." ) ; } format = null ; ElementType . HeaderEntry currentElement = null ; List < Property > currentElementProperties = null ; elements = new ArrayList < ElementType > ( ) ; elementCounts = new HashMap < String , Integer > ( ) ; rawHeaders = new ArrayList < String > ( ) ; for ( String line = hdrReader . readLine ( ) ; true ; line = hdrReader . readLine ( ) ) { if ( line == null ) { throw new IOException ( "Unexpected end of file while " + "reading the header." ) ; } line = line . trim ( ) ; if ( line . isEmpty ( ) ) { continue ; } rawHeaders . add ( line ) ; if ( line . startsWith ( "format " ) ) { if ( format != null ) { throw new IOException ( "Multiple format definitions." ) ; } format = Format . parse ( line ) ; } else if ( line . startsWith ( "element " ) ) { if ( currentElement != null ) { ElementType element = new ElementType ( currentElement . getName ( ) , currentElementProperties ) ; elements . add ( element ) ; elementCounts . put ( currentElement . getName ( ) , currentElement . getCount ( ) ) ; currentElement = null ; currentElementProperties = null ; } currentElement = ElementType . parse ( line ) ; currentElementProperties = new ArrayList < Property > ( ) ; } else if ( line . startsWith ( "property " ) ) { if ( currentElement == null ) { throw new IOException ( "Property without element found." ) ; } Property property = Property . parse ( line ) ; currentElementProperties . add ( property ) ; } else if ( line . startsWith ( "end_header" ) ) { break ; } } if ( currentElement != null ) { ElementType element = new ElementType ( currentElement . getName ( ) , currentElementProperties ) ; elements . add ( element ) ; elementCounts . put ( currentElement . getName ( ) , currentElement . getCount ( ) ) ; currentElement = null ; currentElementProperties = null ; } elements = Collections . unmodifiableList ( elements ) ; if ( format == null ) { throw new IOException ( "Missing format header entry." ) ; } switch ( format ) { case ASCII : this . asciiReader = new BufferedReader ( new InputStreamReader ( stream , Charset . forName ( "US-ASCII" ) ) ) ; this . binaryStream = null ; break ; case BINARY_BIG_ENDIAN : this . asciiReader = null ; this . binaryStream = new BinaryPlyInputStream ( Channels . newChannel ( stream ) , ByteOrder . BIG_ENDIAN ) ; break ; case BINARY_LITTLE_ENDIAN : this . asciiReader = null ; this . binaryStream = new BinaryPlyInputStream ( Channels . newChannel ( stream ) , ByteOrder . LITTLE_ENDIAN ) ; break ; default : throw new IOException ( "Unsupported format: " + format ) ; } }
Reads the header and prepares everything to read the file's content.
@param stream Stream to read the file from.
@throws IOException if an error occurs during reading.