public void GetBytes_ZeroUInt16_Bytes() { byte[] bytes = BytesConverter.GetBytes((ushort)0); Assert.AreEqual(2, bytes.Length); Assert.AreEqual(0, bytes[0]); Assert.AreEqual(0, bytes[1]); }
public void GetBytes_MaxUInt16_Bytes() { byte[] bytes = BytesConverter.GetBytes(ushort.MaxValue); Assert.AreEqual(2, bytes.Length); Assert.AreEqual(255, bytes[0]); Assert.AreEqual(255, bytes[1]); }
public void GetBytes_MaxUInt32_Bytes() { byte[] bytes = BytesConverter.GetBytes(uint.MaxValue); Assert.AreEqual(4, bytes.Length); Assert.AreEqual(255, bytes[0]); Assert.AreEqual(255, bytes[1]); Assert.AreEqual(255, bytes[2]); Assert.AreEqual(255, bytes[3]); }
public void GetBytes_ZeroUInt32_Bytes() { byte[] bytes = BytesConverter.GetBytes((uint)0); Assert.AreEqual(4, bytes.Length); Assert.AreEqual(0, bytes[0]); Assert.AreEqual(0, bytes[1]); Assert.AreEqual(0, bytes[2]); Assert.AreEqual(0, bytes[3]); }
public void GetBytes_MaxUInt64_Bytes() { byte[] bytes = BytesConverter.GetBytes(ulong.MaxValue); Assert.AreEqual(8, bytes.Length); Assert.AreEqual(255, bytes[0]); Assert.AreEqual(255, bytes[1]); Assert.AreEqual(255, bytes[2]); Assert.AreEqual(255, bytes[3]); Assert.AreEqual(255, bytes[4]); Assert.AreEqual(255, bytes[5]); Assert.AreEqual(255, bytes[6]); Assert.AreEqual(255, bytes[7]); }
public void GetBytes_ZeroUInt64_Bytes() { byte[] bytes = BytesConverter.GetBytes((ulong)0); Assert.AreEqual(8, bytes.Length); Assert.AreEqual(0, bytes[0]); Assert.AreEqual(0, bytes[1]); Assert.AreEqual(0, bytes[2]); Assert.AreEqual(0, bytes[3]); Assert.AreEqual(0, bytes[4]); Assert.AreEqual(0, bytes[5]); Assert.AreEqual(0, bytes[6]); Assert.AreEqual(0, bytes[7]); }
/// <inheritdoc/> public IDataStorerEntry Add(string path, params byte[] data) { Validation.NotNull("Path", path); Validation.NotNull("Data", data); int uncompressedLength = data.Length; int compressedLength; byte[] compressedData; CompressionMethod compressionMethod; if (this.enableCompression) { Deflater deflater = new Deflater(Deflater.DEFLATED); deflater.SetInput(data, 0, uncompressedLength); deflater.Finish(); compressedData = new byte[uncompressedLength]; compressedLength = deflater.Deflate(compressedData, 0, uncompressedLength); if (deflater.IsFinished && compressedLength <= uncompressedLength) { // Use deflate compressionMethod = CompressionMethod.Deflate; } else { // Force to store compressedData = data; compressedLength = uncompressedLength; compressionMethod = CompressionMethod.Store; } } else { // Only store compressedData = data; compressedLength = uncompressedLength; compressionMethod = CompressionMethod.Store; } byte[] pathBytes = utf8Encoding.GetBytes(path); this.fileWriter.WriteBytes(BytesConverter.GetBytes((uint)compressedLength)); this.fileWriter.WriteBytes(BytesConverter.GetBytes((uint)pathBytes.Length)); this.fileWriter.WriteBytes((byte)compressionMethod); this.fileWriter.WriteBytes(pathBytes); this.fileWriter.WriteBytes(compressedData, 0, compressedLength); return(new BinaryWriteOnlyStorerEntry(path)); }
public void Send(string text) { if (string.IsNullOrEmpty(text)) { return; } var sendBytes = BytesConverter.GetBytes(text); var codedByteBlocks = CommunicationProtocol.WrapData(sendBytes, _device.SendBlockSize); foreach (var bytes in codedByteBlocks) { _device.Send(bytes); } }
/// <summary> /// Creates a new entry for the <see cref="ZipWriteOnlyStorer"></see>. /// </summary> /// <param name="path"> Path to the data. </param> /// <param name="size"> Size of the data. </param> /// <param name="compressionMethod"> Compression method for the data. </param> /// <param name="compressedSize"> Size of the compressed data. </param> /// <param name="headerOffset"> Offset of header information. </param> /// <param name="crc32"> 32-bit checksum of the data. </param> /// <param name="modifyTime"> Modification time of the data. </param> /// <param name="comment"> User comment for the data. </param> public ZipWriteOnlyStorerEntry(string path, uint size, CompressionMethod compressionMethod, uint compressedSize, uint headerOffset, uint crc32, DateTime modifyTime, string comment) : base(path, compressionMethod, compressedSize, headerOffset, crc32, modifyTime, comment) { this.PathAsBytes = utf8Encoding.GetBytes(path); this.PathLengthAsBytes = BytesConverter.GetBytes((ushort)this.PathAsBytes.Length); this.CommentAsBytes = utf8Encoding.GetBytes(comment); this.CommentLengthAsBytes = BytesConverter.GetBytes((ushort)this.CommentAsBytes.Length); this.CompressionMethodAsBytes = BytesConverter.GetBytes((ushort)compressionMethod); this.ModifyTimeAsBytes = BytesConverter.GetBytes(ZipStorerUtils.DateTimeToDosTime(modifyTime)); this.CRC32AsBytes = BytesConverter.GetBytes(crc32); this.CompressedSizeAsBytes = BytesConverter.GetBytes(compressedSize >= 0xFFFFFFFF ? 0xFFFFFFFF : compressedSize); this.SizeAsBytes = BytesConverter.GetBytes(size >= 0xFFFFFFFF ? 0xFFFFFFFF : size); this.HeaderOffsetAsBytes = BytesConverter.GetBytes(headerOffset >= 0xFFFFFFFF ? 0xFFFFFFFF : (uint)headerOffset); this.CompressedSizeZip64AsBytes = BytesConverter.GetBytes(compressedSize); this.SizeZip64AsBytes = BytesConverter.GetBytes(size); this.HeaderOffsetZip64AsBytes = BytesConverter.GetBytes(headerOffset); }
/// <inheritdoc/> public byte[] Close() { // Writes the central directory header ulong centralOffset = (ulong)this.zipFileWriter.Position; ulong centralSize = 0; long position = 0; for (var node = this.entries.First; node != null; node = node.Next) { position = this.zipFileWriter.Position; this.WriteFileHeader(this.zipFileWriter, node.Value, true); centralSize += (ulong)(this.zipFileWriter.Position - position); } ulong dirOffset = (ulong)this.zipFileWriter.Position; byte[] encodedComment = utf8Encoding.GetBytes(this.comment); if (this.isZip64) { // Writes a ZIP64 end of central directory record (56 bytes) byte[] countBytes = BytesConverter.GetBytes((ulong)this.entries.Count); this.zipFileWriter.WriteBytes(zip64EndOfCentralDirRecordSignatureBytes); // 4 bytes, ZIP64 end of central dir signature this.zipFileWriter.WriteBytes(fourtyFour8Bytes); // 8 bytes, size of ZIP64 end of central directory record. Size = SizeOfFixedFields + SizeOfVariableData - 12. this.zipFileWriter.WriteBytes(fourtyFive2Bytes); // 2 bytes, version made by this.zipFileWriter.WriteBytes(fourtyFive2Bytes); // 2 bytes, version needed to extract this.zipFileWriter.WriteBytes(zero4Bytes); // 4 bytes, number of this disk this.zipFileWriter.WriteBytes(zero4Bytes); // 4 bytes, number of the disk with the start of the central directory this.zipFileWriter.WriteBytes(countBytes); // 8 bytes, total number of entries in the central directory on this disk this.zipFileWriter.WriteBytes(countBytes); // 8 bytes, total number of entries in the central directory this.zipFileWriter.WriteBytes(BytesConverter.GetBytes(centralSize)); // 8 bytes, size of the central directory this.zipFileWriter.WriteBytes(BytesConverter.GetBytes(centralOffset)); // 8 bytes, offset of start of central directory with respect to the starting disk number // Writes the ZIP64 end of central directory locator (20 bytes) this.zipFileWriter.WriteBytes(zip64EndOfCentralDirLocatorSignatureBytes); // 4 bytes, ZIP64 end of central dir locator signature this.zipFileWriter.WriteBytes(zero4Bytes); // 4 bytes, number of the disk with the start of the zip64 end of central directory this.zipFileWriter.WriteBytes(BytesConverter.GetBytes(dirOffset)); // 8 bytes, relative offset of the zip64 end of central directory record this.zipFileWriter.WriteBytes(one4Bytes); // 4 bytes, total number of disks } // Writes the end of central directory record (22 bytes) this.zipFileWriter.WriteBytes(endOfCentralDirRecordSignatureBytes); // 4 bytes, end of central dir signature this.zipFileWriter.WriteBytes(zero2Bytes); // 2 bytes, number of this disk this.zipFileWriter.WriteBytes(zero2Bytes); // 2 bytes, number of the disk with the start of the central directory if (isZip64) { this.zipFileWriter.WriteBytes(zip64EndOfCentralDirectoryDataPart); // 2 + 2 + 4 + 4 = 12 bytes, total number of entries in the central directory on this disk (2),total number of entries in the central directory (2), size of the central directory (4), offset of start of central directory with respect to the starting disk number (4) } else { byte[] countBytes = BytesConverter.GetBytes((ushort)this.entries.Count); this.zipFileWriter.WriteBytes(countBytes); // 2 bytes, total number of entries in the central directory on this disk this.zipFileWriter.WriteBytes(countBytes); // 2 bytes, total number of entries in the central directory this.zipFileWriter.WriteBytes(BytesConverter.GetBytes((uint)centralSize)); // 4 bytes, size of the central directory this.zipFileWriter.WriteBytes(BytesConverter.GetBytes((uint)centralOffset)); // 4 bytes, offset of start of central directory with respect to the starting disk number } this.zipFileWriter.WriteBytes(BytesConverter.GetBytes((ushort)encodedComment.Length)); // 2 bytes, .ZIP file comment length this.zipFileWriter.WriteBytes(encodedComment); // variable size, .ZIP file comment return(this.zipFileWriter.GetBytes()); }