/// <summary> /// Creates and encodes all the partitions to an byte array. /// </summary> /// <param name="buffer">Output byte array.</param> public void EncodePartition(out byte[] buffer) { long alloc = 0; long[] endOffsets = new long[PartitionsDecoded.Count]; for (int k = 0; k < PartitionsDecoded.Count; ++k) { endOffsets[k] = PartitionsDecoded[k].Length; alloc += PartitionsDecoded[k].Length; alloc++; } #if ENABLE_LOCAL_BYTE_IO_ACCESS LocalByteArray output = new LocalByteArray(); #else byte[] output = new byte[alloc]; #endif int pos = -1; foreach (byte[] P in PartitionsDecoded) { foreach (byte p in P) { pos++; output[pos] = Format255ByteTo128Unsigned(p); } pos++; output[pos] = 255; } buffer = output; #if ENABLE_LOCAL_BYTE_IO_ACCESS output.Dispose(); #endif }
internal void DecodePartition(byte[] buffer) { #if ENABLE_LOCAL_BYTE_IO_ACCESS List <LocalByteArray> partitions = new List <LocalByteArray>(); LocalByteArray tmp_Crt = new LocalByteArray(); #else List <byte[]> partitions = new List <byte[]>(); List <byte> tmp_Crt = new List <byte>(); #endif for (int pos = 0; pos < buffer.Length; pos++) { byte by = buffer[pos]; // end of stream if (pos == buffer.Length - 1) { partitions.Add(tmp_Crt.ToArray()); break; } if (by == 255) { // partition divisor partitions.Add(tmp_Crt.ToArray()); #if ENABLE_LOCAL_BYTE_IO_ACCESS tmp_Crt.Dispose(); tmp_Crt = new LocalByteArray(); #else tmp_Crt.Clear(); tmp_Crt = new List <byte>(); #endif #if USE_OPTIMIZATIONS GC.Collect(); GC.WaitForPendingFinalizers(); // wait for memory clear #endif continue; } else { byte n = Format128UnsignedTo255Byte(by); tmp_Crt.Add(n); continue; } } #if ENABLE_LOCAL_BYTE_IO_ACCESS foreach (byte[] tmp in partitions) { this.PartitionsDecoded.Add(tmp); } #else this.PartitionsDecoded.AddRange(partitions); #endif #if ENABLE_LOCAL_BYTE_IO_ACCESS foreach (LocalByteArray n in partitions) { n.Dispose(); } partitions.Clear(); tmp_Crt.Dispose(); #else partitions.Clear(); partitions = null; tmp_Crt.Clear(); tmp_Crt = null; #endif GC.Collect(); }