kinow commented on a change in pull request #38: Cleaned up PNG component.
URL: https://github.com/apache/commons-imaging/pull/38#discussion_r284485317
 
 

 ##########
 File path: 
src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
 ##########
 @@ -100,119 +161,97 @@ public String getDefaultExtension() {
 
     // private final static int tRNS = CharsToQuad('t', 'R', 'N', 's');
 
-    public static String getChunkTypeName(final int chunkType) {
-        final StringBuilder result = new StringBuilder();
-        result.append((char) (0xff & (chunkType >> 24)));
-        result.append((char) (0xff & (chunkType >> 16)));
-        result.append((char) (0xff & (chunkType >> 8)));
-        result.append((char) (0xff & (chunkType >> 0)));
-        return result.toString();
+    public static String getChunkTypeName(final int chunkTypeCode) {
+        return new String(
+                ByteBuffer.allocate(4).putInt(chunkTypeCode).array(),
+                StandardCharsets.ISO_8859_1);
     }
 
     /**
+     * Extracts all the chunks in the PNG file (in the input stream)
+     * and returns a list of their names.
+     * 
+     * @param is
      * @return List of String-formatted chunk types, ie. "tRNs".
+     * @throws ImageReadException
+     * @throws IOException
      */
     public List<String> getChunkTypes(final InputStream is)
             throws ImageReadException, IOException {
         final List<PngChunk> chunks = readChunks(is, null, false);
         final List<String> chunkTypes = new ArrayList<>(chunks.size());
         for (final PngChunk chunk : chunks) {
-            chunkTypes.add(getChunkTypeName(chunk.chunkType));
+            chunkTypes.add(getChunkTypeName(chunk.chunkType.toCode()));
         }
         return chunkTypes;
     }
 
     public boolean hasChunkType(final ByteSource byteSource, final ChunkType 
chunkType)
             throws ImageReadException, IOException {
-        try (InputStream is = byteSource.getInputStream()) {
-            readSignature(is);
-            final List<PngChunk> chunks = readChunks(is, new ChunkType[] { 
chunkType }, true);
-            return !chunks.isEmpty();
-        }
-    }
-
-    private boolean keepChunk(final int chunkType, final ChunkType[] 
chunkTypes) {
-        // System.out.println("keepChunk: ");
-        if (chunkTypes == null) {
-            return true;
-        }
-
-        for (final ChunkType chunkType2 : chunkTypes) {
-            if (chunkType2.value == chunkType) {
-                return true;
-            }
-        }
-        return false;
+        return ChunkReader.newInstance(byteSource)
+                .readChunks(new ChunkUniselector(true, chunkType), true)
+                .size() > 0;
     }
 
-    private List<PngChunk> readChunks(final InputStream is, final ChunkType[] 
chunkTypes,
-            final boolean returnAfterFirst) throws ImageReadException, 
IOException {
+    /**
+     * Reads all the chunks (satisfying any filters) from the input
+     * stream containing a PNG file.
+     * 
+     * Replaces the readChunks(InputStream, ChunkTypes[], boolean)
+     * method. Is clean, and readable now.
+     * 
+     * @param inputStream
+     * @param selector
+     * @param returnAfterFirst
+     * @return
+     * @throws IOException
+     * @throws ImageReadException 
+     */
+    private List<PngChunk> readChunks(final InputStream inputStream,
+            final ChunkSelector selector, final boolean returnAfterFirst)
+            throws IOException, ImageReadException {
         final List<PngChunk> result = new ArrayList<>();
-
-        while (true) {
-            final int length = read4Bytes("Length", is, "Not a Valid PNG 
File", getByteOrder());
-            final int chunkType = read4Bytes("ChunkType", is, "Not a Valid PNG 
File", getByteOrder());
-
-            if (LOGGER.isLoggable(Level.FINEST)) {
-                printCharQuad("ChunkType", chunkType);
-                debugNumber("Length", length, 4);
-            }
-            final boolean keep = keepChunk(chunkType, chunkTypes);
-
-            byte[] bytes = null;
-            if (keep) {
-                bytes = readBytes("Chunk Data", is, length,
-                        "Not a Valid PNG File: Couldn't read Chunk Data.");
-            } else {
-                skipBytes(is, length, "Not a Valid PNG File");
-            }
-
-            if (LOGGER.isLoggable(Level.FINEST)) {
-                if (bytes != null) {
-                    debugNumber("bytes", bytes.length, 4);
-                }
-            }
-
-            final int crc = read4Bytes("CRC", is, "Not a Valid PNG File", 
getByteOrder());
-
-            if (keep) {
-                if (chunkType == ChunkType.iCCP.value) {
-                    result.add(new PngChunkIccp(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.tEXt.value) {
-                    result.add(new PngChunkText(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.zTXt.value) {
-                    result.add(new PngChunkZtxt(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.IHDR.value) {
-                    result.add(new PngChunkIhdr(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.PLTE.value) {
-                    result.add(new PngChunkPlte(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.pHYs.value) {
-                    result.add(new PngChunkPhys(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.sCAL.value) {
-                    result.add(new PngChunkScal(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.IDAT.value) {
-                    result.add(new PngChunkIdat(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.gAMA.value) {
-                    result.add(new PngChunkGama(length, chunkType, crc, 
bytes));
-                } else if (chunkType == ChunkType.iTXt.value) {
-                    result.add(new PngChunkItxt(length, chunkType, crc, 
bytes));
-                } else {
-                    result.add(new PngChunk(length, chunkType, crc, bytes));
+        final DataInputStream dataInput = new DataInputStream(inputStream);
+        
+        while(true) {
+            final ChunkLoader nextChunkLoaded = ChunkLoader.run(dataInput, 
selector);
+            PngChunk loadedChunk = nextChunkLoaded.loadedChunk();
+            
+            if(loadedChunk != null) {
+                { // (Shukant Pal): I essentially hate how developers
 
 Review comment:
   Author name not needed. And see note about this kind of note in a previous 
comment. We must be polite and nice to each developer, no matter how good or 
bad their code can be.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to