private AbstractFileInfo readHeader()

in src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java [207:308]


    private AbstractFileInfo readHeader(final InputStream inputStream) throws ImagingException, IOException {
        final byte identifier1 = readByte("Identifier1", inputStream, "Not a Valid PNM File");
        final byte identifier2 = readByte("Identifier2", inputStream, "Not a Valid PNM File");

        if (identifier1 != PnmConstants.PNM_PREFIX_BYTE) {
            throw new ImagingException("PNM file has invalid prefix byte 1");
        }

        final WhiteSpaceReader wsReader = new WhiteSpaceReader(inputStream);

        if (identifier2 == PnmConstants.PBM_TEXT_CODE || identifier2 == PnmConstants.PBM_RAW_CODE || identifier2 == PnmConstants.PGM_TEXT_CODE
                || identifier2 == PnmConstants.PGM_RAW_CODE || identifier2 == PnmConstants.PPM_TEXT_CODE || identifier2 == PnmConstants.PPM_RAW_CODE) {

            final int width;
            try {
                width = Integer.parseInt(wsReader.readtoWhiteSpace());
            } catch (final NumberFormatException e) {
                throw new ImagingException("Invalid width specified.", e);
            }
            final int height;
            try {
                height = Integer.parseInt(wsReader.readtoWhiteSpace());
            } catch (final NumberFormatException e) {
                throw new ImagingException("Invalid height specified.", e);
            }

            switch (identifier2) {
            case PnmConstants.PBM_TEXT_CODE:
                return new PbmFileInfo(width, height, false);
            case PnmConstants.PBM_RAW_CODE:
                return new PbmFileInfo(width, height, true);
            case PnmConstants.PGM_TEXT_CODE: {
                final int maxgray = Integer.parseInt(wsReader.readtoWhiteSpace());
                return new PgmFileInfo(width, height, false, maxgray);
            }
            case PnmConstants.PGM_RAW_CODE: {
                final int maxgray = Integer.parseInt(wsReader.readtoWhiteSpace());
                return new PgmFileInfo(width, height, true, maxgray);
            }
            case PnmConstants.PPM_TEXT_CODE: {
                final int max = Integer.parseInt(wsReader.readtoWhiteSpace());
                return new PpmFileInfo(width, height, false, max);
            }
            case PnmConstants.PPM_RAW_CODE: {
                final int max = Integer.parseInt(wsReader.readtoWhiteSpace());
                return new PpmFileInfo(width, height, true, max);
            }
            default:
                break;
            }
        } else if (identifier2 == PnmConstants.PAM_RAW_CODE) {
            int width = -1;
            int height = -1;
            int depth = -1;
            int maxVal = -1;
            final StringBuilder tupleType = new StringBuilder();

            // Advance to next line
            wsReader.readLine();
            String line;
            while ((line = wsReader.readLine()) != null) {
                line = line.trim();
                if (line.charAt(0) == '#') {
                    continue;
                }
                final StringTokenizer tokenizer = new StringTokenizer(line, " ", false);
                final String type = tokenizer.nextToken();
                switch (type) {
                case TOKEN_WIDTH:
                    width = checkNextTokensAsInt(tokenizer, type);
                    break;
                case TOKEN_HEIGHT:
                    height = checkNextTokensAsInt(tokenizer, type);
                    break;
                case TOKEN_DEPTH:
                    depth = checkNextTokensAsInt(tokenizer, type);
                    break;
                case TOKEN_MAXVAL:
                    maxVal = checkNextTokensAsInt(tokenizer, type);
                    break;
                case TOKEN_TUPLTYPE:
                    tupleType.append(checkNextTokens(tokenizer, type));
                    break;
                case TOKEN_ENDHDR:
                    // consumed & noop
                    break;
                default:
                    throw new ImagingException("Invalid PAM file header type " + type);
                }
                if (TOKEN_ENDHDR.equals(type)) {
                    break;
                }
            }
            checkFound(width, TOKEN_WIDTH);
            checkFound(height, TOKEN_HEIGHT);
            checkFound(depth, TOKEN_DEPTH);
            checkFound(maxVal, TOKEN_MAXVAL);
            check(tupleType.length() > 0, TOKEN_TUPLTYPE);
            return new PamFileInfo(width, height, depth, maxVal, tupleType.toString());
        }
        throw new ImagingException("PNM file has invalid prefix byte 2");
    }