public KinectFramesStore DeserializeAndLoadFileChunk(string fileName, long chunkStartPosition, long chunkSize) { // Open the requested save file, seek to the starting position of the chunk and read in data // up to the chunk size. Then, decompress this byte array and write it to a memory stream // so that this can be passed to the formatter and deserialized into a frame store object KinectFramesStore chunk; BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { using (FileStream fileStream = new FileStream(GetFullFilePath(fileName), FileMode.Open)) { byte[] compressedChunk = new byte[chunkSize]; fileStream.Seek(chunkStartPosition, SeekOrigin.Begin); fileStream.Read(compressedChunk, 0, compressedChunk.Length); byte[] decompressedChunk = QuickLZ.decompress(compressedChunk); memoryStream.Write(decompressedChunk, 0, decompressedChunk.Length); } memoryStream.Position = 0; chunk = (KinectFramesStore)formatter.Deserialize(memoryStream); } return(chunk); }
public void SerializeAndSaveFileChunk(string fileName, KinectFramesStore serializableData) { // Check that the specified file exists if (!DoesFileExist(fileName)) { throw new FileNotFoundException("Could not find the specified file"); } // Use a binary formatter and memory stream to first serialize the incoming // chunk. Then, use QuickLZ to compress this data before writing it into the // file BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { try { formatter.Serialize(memoryStream, serializableData); } catch (SerializationException) { throw new SerializationException("Error in serializing kinect data"); } // Reset the memory stream to its beginning and then compress the serialized object. // Level 1 gives a higher compression ration and Level 3 gives faster decompression memoryStream.Position = 0; byte[] compressedBytes = QuickLZ.compress(memoryStream.ToArray(), 3); // Append the chunk onto the file, also recording the size of the new chunk using the // stream position before and after the write so that it can be stored in the file info // object long startPosition, endPosition; using (FileStream fileStream = new FileStream(GetFullFilePath(fileName), FileMode.Append, FileAccess.Write)) { startPosition = fileStream.Position; try { fileStream.Write(compressedBytes, 0, compressedBytes.Length); } catch (IOException) { throw new IOException("Could not write chunk data to file"); } endPosition = fileStream.Position; } // Add a new chunk size to the file info object's internal list using the stream // position differences fileInfo.ChunkSizes.Add(endPosition - startPosition); } }
public void SerializeAndSaveFileInfo(string fileName, float recordingLengthInSecs) { // Check that the specified file exists and that the recording length input is valid if (!DoesFileExist(fileName)) { throw new FileNotFoundException("Could not find the specified file"); } else if (recordingLengthInSecs < 0.0f) { throw new ArgumentOutOfRangeException("Recording length must be a positive value"); } // Add the final recording length to the file info object to complete it fileInfo.TotalRecordingLength = recordingLengthInSecs; long startPosition, endPosition; string fullFilePath = GetFullFilePath(fileName); // Use similar code to the saving of chunks to serialize the data, compress it and then // append it onto the end of the save file BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { try { formatter.Serialize(memoryStream, fileInfo); } catch (SerializationException) { throw new SerializationException("Error in serializing file information"); } memoryStream.Position = 0; byte[] compressedBytes = QuickLZ.compress(memoryStream.ToArray(), 3); using (FileStream fileStream = new FileStream(fullFilePath, FileMode.Append, FileAccess.Write)) { startPosition = fileStream.Position; try { fileStream.Write(compressedBytes, 0, compressedBytes.Length); } catch (IOException) { throw new IOException("Could not write file information to file"); } endPosition = fileStream.Position; } } // Finally, reopen the file using a binary writer and append the file info object size onto its end. // The file has to be reopened like this because opening multiple writers on the same stream causes // errors using (BinaryWriter writer = new BinaryWriter(File.Open(fullFilePath, FileMode.Append, FileAccess.Write))) { try { writer.Write(endPosition - startPosition); } catch (IOException) { throw new IOException("Error in writing file footer size to the file"); } } }
public FileInfo DeserializeAndLoadFileInfo(string fileName) { // Check that the file to deserialize exists if (!DoesFileExist(fileName)) { throw new FileNotFoundException("Could not find the specified file"); } // Open the recording file, move the position in the stream to the head of the last long value // in the file and read it back using a binary reader. Then, use its value to set the position // of the file stream to the head of the compressed info footer and read this back into a byte // array FileInfo fileInfo; byte[] fileInfoCompressed; using (FileStream fileStream = new FileStream(GetFullFilePath(fileName), FileMode.Open)) { try { fileStream.Seek(-sizeof(long), SeekOrigin.End); } catch (ArgumentException) { throw new ArgumentOutOfRangeException("File is smaller than minimum size; is it a " + fileExtension + " file?"); } long footerSize; using (BinaryReader reader = new BinaryReader(fileStream, Encoding.UTF8, true)) { try { footerSize = reader.ReadInt64(); } catch (IOException) { throw new IOException("Could not read file info footer size"); } } try { fileStream.Seek(-(sizeof(long) + footerSize), SeekOrigin.End); } catch (ArgumentOutOfRangeException) { throw new ArgumentOutOfRangeException("File is smaller than minimum size; is it a .dvrec file?"); } fileInfoCompressed = new byte[footerSize]; fileStream.Read(fileInfoCompressed, 0, fileInfoCompressed.Length); } // Using a binary formatter, memory stream and the quick lz class, decompress the byte array and write // it to the memory stream. Then, use this to deserialize the file info object, storing a reference to // it and then returning this BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { byte[] decompressedBytes = QuickLZ.decompress(fileInfoCompressed); memoryStream.Write(decompressedBytes, 0, decompressedBytes.Length); memoryStream.Position = 0; fileInfo = (FileInfo)formatter.Deserialize(memoryStream); } return(fileInfo); }