/// <summary> /// Writes the media object to a stream. /// </summary> /// <param name="mediaId">The media id.</param> /// <param name="output">The output.</param> private void WriteMedia(int mediaId, Stream output) { using (NpgsqlConnection conn = FileHandlerHelpers.GetPgConnection()) { int noid = 0; using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;"; cmd.Parameters.Add("id", mediaId); noid = Convert.ToInt32(cmd.ExecuteScalar()); } NpgsqlTransaction tran = conn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(conn); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); largeObject.Seek(0); int size = largeObject.Size(); byte[] buffer = new byte[size]; int read = 0; int offset = 0; while (offset < size) { read = largeObject.Read(buffer, offset, Math.Min(102400, size - offset)); output.Write(buffer, offset, read); offset += 102400; } largeObject.Close(); tran.Commit(); } }
private readonly int chunkSize = 204800; //200 KB /// <summary> /// Writes the content of a buffer into a LargeObject. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="largeObject">The large object.</param> /// <remarks>Documented by Dev02, 2008-08-08</remarks> private void BufferToLargeObject(byte[] buffer, LargeObject largeObject) { largeObject.Seek(0); int offset = 0; int size = buffer.Length; while (offset < size) { largeObject.Write(buffer, offset, Math.Min(chunkSize, size - offset)); offset += chunkSize; } }
/// <summary> /// Gets the contents of a LargeObject into a buffer. /// </summary> /// <param name="largeObject">The large object.</param> /// <returns></returns> /// <remarks>Documented by Dev02, 2008-08-08</remarks> private byte[] LargeObjectToBuffer(LargeObject largeObject) { largeObject.Seek(0); int size = largeObject.Size(); byte[] buffer = new byte[size]; int offset = 0; while (offset < size) { largeObject.Read(buffer, offset, Math.Min(chunkSize, size - offset)); offset += chunkSize; } return(buffer); }
/// <summary> /// Writes the content of a buffer into a LargeObject. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="largeObject">The large object.</param> /// <param name="rpu">The rpu.</param> /// <param name="caller">The calling object.</param> /// <remarks>Documented by Dev02, 2008-08-08</remarks> private void BufferToLargeObject(byte[] buffer, LargeObject largeObject, StatusMessageReportProgress rpu, object caller) { largeObject.Seek(0); int offset = 0; int size = buffer.Length; StatusMessageEventArgs args = new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, buffer.Length); while (offset < size) { largeObject.Write(buffer, offset, Math.Min(chunkSize, size - offset)); offset += chunkSize; args.Progress = offset; if (rpu != null) { rpu(args, caller); } } }