Esempio n. 1
0
  /**
   * 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;
    }
  }