/** * Read the next PNG chunk from the input stream given. If unable to read a chunk, return null. */ static PngChunk readNextFromStream(LEDataInputStream stream) { try { int headerLength = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH; byte[] headerBytes = new byte[headerLength]; int result = stream.read(headerBytes, 0, headerLength); stream.unread(headerBytes); if (result != headerLength) return null; PngChunk tempChunk = new PngChunk(headerBytes); int chunkLength = tempChunk.getSize(); byte[] chunk = new byte[chunkLength]; result = stream.read(chunk, 0, chunkLength); if (result != chunkLength) return null; switch (tempChunk.getChunkType()) { case CHUNK_IHDR: return new PngIhdrChunk(chunk); case CHUNK_PLTE: return new PngPlteChunk(chunk); case CHUNK_IDAT: return new PngIdatChunk(chunk); case CHUNK_IEND: return new PngIendChunk(chunk); case CHUNK_tRNS: return new PngTrnsChunk(chunk); default: return new PngChunk(chunk); } } catch (IOException e) { return null; } }
@Override boolean isFileFormat(LEDataInputStream stream) { try { byte[] signature = new byte[3]; stream.read(signature); stream.unread(signature); return signature[0] == 'G' && signature[1] == 'I' && signature[2] == 'F'; } catch (Exception e) { return false; } }
boolean isFileFormat(LEDataInputStream stream) { try { byte[] header = new byte[18]; stream.read(header); stream.unread(header); int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize == BMPHeaderFixedSize; } catch (Exception e) { return false; } }