private void GetHeader(WrapReader reader) { Dictionary <byte, string> statusLib = new Dictionary <byte, string>(); List <byte> keyFieldList = new List <byte>(); string key = string.Empty; byte currentByte = 0; byte value; byte split = (byte)StatusMark.Split; while ((currentByte = reader.ReadByte()) != (byte)'\n') { if (currentByte == split) { key = Encoding.UTF8.GetString(keyFieldList.ToArray()); value = reader.ReadByte(); statusLib[value] = key; keyFieldList = new List <byte>(); } else { keyFieldList.Add(currentByte); } } libValueToStatus = statusLib; }
private void WriteString(WrapReader reader, BinaryWriter writer) { byte endString = (byte)TypeOfWriteData.EndString; byte currentByte; while ((currentByte = reader.ReadByte()) != endString) { writer.Write(currentByte); } }
private void WriteRandomBytesArray(WrapReader reader, BinaryWriter writer, ref bool typeAlreadyRead, ref TypeOfWriteData typeWriteData) { byte endString = (byte)TypeOfWriteData.EndString; List <byte> bytesToWrite = new List <byte>(); do { byte currentByte = reader.ReadByte(); if (currentByte == endString) { currentByte = reader.ReadByte(); if (currentByte != endString) { // Save information about type next string typeAlreadyRead = false; typeWriteData = (TypeOfWriteData)currentByte; break; } } bytesToWrite.Add(currentByte); } while (true); writer.BaseStream.Write(bytesToWrite.ToArray()); }
private void WriteStringLogFormat(WrapReader reader, BinaryWriter writer) { byte currentByte; long integerOfSecondBlock; long dateTicks = reader.ReadInt64(); string statusWord = string.Empty; DateTime date = new DateTime(dateTicks); // Get Integer (2-nd block) var: integerOfSecondBlock if (isFirstString || reader.ReadByte() == 1) { integerOfSecondBlock = isFirstString ? reader.ReadInt64() : GetMs(new DateTime(dateTicks - tickFromStart)) + millisecondsFromStarts - 1; } else { integerOfSecondBlock = reader.ReadInt64(); } // Save information about integer of second block if (isFirstString) { isFirstString = false; tickFromStart = dateTicks; millisecondsFromStarts = integerOfSecondBlock; } // Get status (3-rd block) var: statusWord if (reader.ReadByte() == 1) { // Value exist in library byte keyForLib = reader.ReadByte(); statusWord = libValueToStatus[keyForLib]; } else { List <byte> statusWordBytes = new List <byte>(); byte EndStatusWord = (byte)StatusMark.EndStatusWord; while ((currentByte = reader.ReadByte()) != EndStatusWord) { statusWordBytes.Add(currentByte); } statusWord = Encoding.UTF8.GetString(statusWordBytes.ToArray()); } byte endStringByte = (byte)TypeOfWriteData.EndString; StringBuilder rightPart = new StringBuilder(); while ((currentByte = reader.ReadByte()) != endStringByte) { rightPart.Append((char)currentByte); } long currentPoint = writer.BaseStream.Position; writer.BaseStream.Write(Encoding.UTF8.GetBytes(date.Year + "-" + AddLeadingZeros(date.Month) + "-" + AddLeadingZeros(date.Day) + " " + AddLeadingZeros(date.Hour) + ":" + AddLeadingZeros(date.Minute) + ":" + AddLeadingZeros(date.Second) + "," + AddLeadingZeros(date.Millisecond, 3))); writer.BaseStream.Write(Encoding.UTF8.GetBytes(" " + integerOfSecondBlock)); writer.BaseStream.Write(Encoding.UTF8.GetBytes(new string(' ', Math.Max(1, 31 - ((int)writer.BaseStream.Position - (int)currentPoint))))); writer.BaseStream.Write(Encoding.UTF8.GetBytes(statusWord)); writer.BaseStream.Write(Encoding.UTF8.GetBytes(new string(' ', Math.Max(1, 37 - ((int)writer.BaseStream.Position - (int)currentPoint))))); writer.BaseStream.Write(Encoding.UTF8.GetBytes(rightPart.ToString())); }
public void Decompress() { libValueToStatus = new Dictionary <byte, string>(); using (var fileRead = File.Open(inputFileName, FileMode.Open)) using (GZipStream zipReader = new GZipStream(fileRead, CompressionMode.Decompress, true)) using (var mReader = new MemoryStream()) using (var _reader = new BinaryReader(mReader)) { using (MemoryStream mWriter = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(mWriter)) { WrapReader reader = new WrapReader(_reader, zipReader, mReader); bool typeOfWriteDataNoReaded = true; TypeOfWriteData typeData = TypeOfWriteData.String; do { if (typeOfWriteDataNoReaded) { typeData = (TypeOfWriteData)reader.ReadByte(); } else { typeOfWriteDataNoReaded = true; } if (typeData == TypeOfWriteData.StringLogFormat) { WriteStringLogFormat(reader, writer); } else if (typeData == TypeOfWriteData.String) { WriteString(reader, writer); } else if (typeData == TypeOfWriteData.RandomBytes) { WriteRandomBytesArray(reader, writer, ref typeOfWriteDataNoReaded, ref typeData); } else if (typeData == TypeOfWriteData.Header) { GetHeader(reader); } else { break; } } while (true); mWriter.Position = 0; FileMode fMode = File.Exists(outputFileName) ? FileMode.Truncate : FileMode.Create; using (var file = File.Open(outputFileName, fMode)) { if (File.Exists(tmpFileName)) { using (var tmp = File.Open(tmpFileName, FileMode.Open)) tmp.CopyTo(file); File.Delete(tmpFileName); } mWriter.CopyTo(file); } } } }