public static T Clone <T>(T obj) where T : class, ISerializeBinary { T clone; // todo: don't use AcquireBuffer here! using (var buffer = BufferManager.AcquireBuffer()) { using (var ms = new MemoryStream(buffer.Data, true)) { ms.Position = 0; using (var writer = new BinaryWriter(ms)) { writer.Write(obj); } } using (var ms = new MemoryStream(buffer.Data)) { ms.Position = 0; using (var reader = new BinaryReader(ms)) { clone = reader.ReadObject <T>(); } } } return(clone); }
public FileStreamUnbufferedSequentialWrite(string path, long length, long startPosition, bool truncateOnClose) { m_path = path; m_id = IdentityManager.AcquireIdentity(string.Format("{0}:{1}", this.GetType().Name, m_path)); m_buffer = BufferManager.AcquireBuffer(m_id, true); m_sectorSize = PathUtil.GetDriveSectorSize(m_path); m_length = length; m_lengthAligned = (m_length + (m_sectorSize - 1)) & (~(long)(m_sectorSize - 1)); m_truncateOnClose = truncateOnClose; const FileMode mode = FileMode.OpenOrCreate; const FileAccess access = FileAccess.Write; const FileShare share = FileShare.None; const FileOptions options = (FileFlagNoBuffering | FileOptions.WriteThrough | FileOptions.SequentialScan); m_stream = new FileStream(m_path, mode, access, share, BUFFER_SIZE, options); m_stream.SetLength(m_lengthAligned); long startPositionAligned = ((startPosition + (m_sectorSize - 1)) & (~(long)(m_sectorSize - 1))) - m_sectorSize; if (startPositionAligned >= 0) { m_stream.Seek(startPositionAligned, SeekOrigin.Begin); } else { startPositionAligned = 0; } m_bufferIndex = (int)(startPosition - startPositionAligned); }
public FileStreamUnbufferedSequentialRead(string path, long startPosition) { m_path = path; m_id = IdentityManager.AcquireIdentity(string.Format("{0}:{1}", GetType().Name, m_path)); m_buffer = BufferManager.AcquireBuffer(m_id, true); m_sectorSize = PathUtil.GetDriveSectorSize(m_path); m_bufferValidSize = m_buffer.Length; const FileMode mode = FileMode.Open; const FileAccess access = FileAccess.Read; const FileShare share = FileShare.Read; const FileOptions options = (FileFlagNoBuffering | FileOptions.WriteThrough | FileOptions.SequentialScan); m_stream = new FileStream(m_path, mode, access, share, BUFFER_SIZE, options); m_streamEnd = new FileStream(m_path, mode, access, share, BUFFER_SIZE, FileOptions.WriteThrough); Seek(startPosition); }
public BufferInstance AcquireBuffer(int size, bool pin) { return(BufferManager.AcquireBuffer(m_id, size, pin)); }
public BufferInstance AcquireBuffer(bool pin) { return(BufferManager.AcquireBuffer(m_id, pin)); }