static public void testReadall() { long t0 = Environment.TickCount; for (int n = 0; n < 2000; n++) { String f = "C:/temp/z/zlibcs" + n + ".bin"; Stream bos = new FileStream(f, FileMode.Open); AZlibInputStream ist = ZlibStreamFactory.createZlibInputStream(bos, false); if (n == 0) { Console.WriteLine("Using: " + ist); } byte[] res = readAll(ist); long expectedlen = n < 50 ? n : n * n - 7; if (res.Length != expectedlen) { throw new Exception("error 0: " + f + " expected:" + expectedlen + " len=" + res.Length); } for (int i = 0; i < expectedlen; i++) { if ((res[i] & 0xff) != ((i * 7) & 0xff)) { throw new Exception("error 1: " + f); } } //Console.WriteLine(f + " :" + expectedlen); } long t1 = Environment.TickCount; Console.WriteLine("read 3000 files in " + (t1 - t0)); }
internal static byte[] compressBytes(byte[] ori, int offset, int len, bool compress) { try { MemoryStream inb = new MemoryStream(ori, offset, len); Stream inx = inb; if (!compress) { inx = ZlibStreamFactory.createZlibInputStream(inb); } MemoryStream outb = new MemoryStream(); Stream outx = outb; if (compress) { outx = ZlibStreamFactory.createZlibOutputStream(outb); } shovelInToOut(inx, outx); inx.Close(); outx.Close(); byte[] res = outb.ToArray(); return(res); } catch (Exception e) { throw new PngjException(e); } }
internal static byte[] compressBytes(byte[] ori, int offset, int len, bool compress) { try { MemoryStream memoryStream = new MemoryStream(ori, offset, len); Stream stream = memoryStream; if (!compress) { stream = ZlibStreamFactory.createZlibInputStream(memoryStream); } MemoryStream memoryStream2 = new MemoryStream(); Stream stream2 = memoryStream2; if (compress) { stream2 = ZlibStreamFactory.createZlibOutputStream(memoryStream2); } shovelInToOut(stream, stream2); stream.Dispose(); stream2.Dispose(); return(memoryStream2.ToArray()); } catch (Exception cause) { throw new PngjException(cause); } }
/// <summary> /// Reads chunks before first IDAT. Position before: after IDHR (crc included) /// Position after: just after the first IDAT chunk id Returns length of first /// IDAT chunk , -1 if not found /// </summary> /// private void ReadFirstChunks() { if (!FirstChunksNotYetRead()) { return; } int clen = 0; bool found = false; byte[] chunkid = new byte[4]; // it's important to reallocate in each this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR; while (!found) { clen = PngHelperInternal.ReadInt4(inputStream); offset += 4; if (clen < 0) { break; } PngHelperInternal.ReadBytes(inputStream, chunkid, 0, 4); offset += 4; if (PngCsUtils.arraysEqual4(chunkid, Hjg.Pngcs.Chunks.ChunkHelper.b_IDAT)) { found = true; this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_4_IDAT; // add dummy idat chunk to list chunksList.AppendReadChunk(new PngChunkIDAT(ImgInfo, clen, offset - 8), CurrentChunkGroup); break; } else if (PngCsUtils.arraysEqual4(chunkid, Hjg.Pngcs.Chunks.ChunkHelper.b_IEND)) { throw new PngjInputException("END chunk found before image data (IDAT) at offset=" + offset); } String chunkids = ChunkHelper.ToString(chunkid); if (chunkids.Equals(ChunkHelper.PLTE)) { this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_2_PLTE; } ReadChunk(chunkid, clen, false); if (chunkids.Equals(ChunkHelper.PLTE)) { this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_3_AFTERPLTE; } } int idatLen = found ? clen : -1; if (idatLen < 0) { throw new PngjInputException("first idat chunk not found!"); } iIdatCstream = new PngIDatChunkInputStream(inputStream, idatLen, offset); idatIstream = ZlibStreamFactory.createZlibInputStream(iIdatCstream, true); if (!crcEnabled) { iIdatCstream.DisableCrcCheck(); } }
public void ReadFirstChunks() { if (!FirstChunksNotYetRead()) { return; } int num = 0; bool flag = false; byte[] array = new byte[4]; CurrentChunkGroup = 1; while (!flag) { num = PngHelperInternal.ReadInt4(inputStream); offset += 4L; if (num < 0) { break; } PngHelperInternal.ReadBytes(inputStream, array, 0, 4); offset += 4L; if (PngCsUtils.arraysEqual4(array, ChunkHelper.b_IDAT)) { flag = true; CurrentChunkGroup = 4; chunksList.AppendReadChunk(new PngChunkIDAT(ImgInfo, num, offset - 8), CurrentChunkGroup); break; } if (PngCsUtils.arraysEqual4(array, ChunkHelper.b_IEND)) { throw new PngjInputException("END chunk found before image data (IDAT) at offset=" + offset.ToString()); } string text = ChunkHelper.ToString(array); if (text.Equals("PLTE")) { CurrentChunkGroup = 2; } ReadChunk(array, num, skipforced: false); if (text.Equals("PLTE")) { CurrentChunkGroup = 3; } } int num2 = flag ? num : (-1); if (num2 < 0) { throw new PngjInputException("first idat chunk not found!"); } iIdatCstream = new PngIDatChunkInputStream(inputStream, num2, offset); idatIstream = ZlibStreamFactory.createZlibInputStream(iIdatCstream, leaveOpen: true); if (!crcEnabled) { iIdatCstream.DisableCrcCheck(); } }