private static void TestWrite(Stream stream, ByteOrder byteOrder) { var output = new BufferedStreamWriter(stream); var dataWriter = new BinaryDataWriter(output, byteOrder); Trace.Assert(stream.Position == 0); dataWriter.Write(TestByte); output.Flush(FlushMode.Shallow); Trace.Assert(stream.Position == 1); dataWriter.Write(TestShort); dataWriter.Write(TestUShort); Trace.Assert(stream.Position == 1); output.Flush(FlushMode.Shallow); Trace.Assert(stream.Position == 5); dataWriter.Write(TestInt); dataWriter.Write(TestUInt); dataWriter.Write(TestLong); dataWriter.Write(TestULong); dataWriter.Write(TestFloat); dataWriter.Write(TestDouble); Trace.Assert(stream.Position == 5); output.Flush(FlushMode.Shallow); Trace.Assert(stream.Position == 41); TestWriteArray(9 * ushort.MaxValue, TestByte, dataWriter.Write); TestWriteArray(8 * ushort.MaxValue, TestShort, dataWriter.Write); TestWriteArray(7 * ushort.MaxValue, TestUShort, dataWriter.Write); TestWriteArray(6 * ushort.MaxValue, TestInt, dataWriter.Write); TestWriteArray(5 * ushort.MaxValue, TestUInt, dataWriter.Write); TestWriteArray(4 * ushort.MaxValue, TestLong, dataWriter.Write); TestWriteArray(3 * ushort.MaxValue, TestULong, dataWriter.Write); TestWriteArray(2 * ushort.MaxValue, TestFloat, dataWriter.Write); TestWriteArray(1 * ushort.MaxValue, TestDouble, dataWriter.Write); output.Flush(FlushMode.Deep); }
protected static void WriteLengthPrefixedData(BinaryDataWriter dataWriter, Action <BinaryDataWriter, long> lengthWriter, Action <BinaryDataWriter> writeDataAction) { using (var dataStream = new MemoryStream()) { var dataOutput = new BufferedStreamWriter(dataStream); writeDataAction.Invoke(new BinaryDataWriter(dataOutput, ByteOrder.BigEndian)); dataOutput.Flush(FlushMode.Shallow); lengthWriter.Invoke(dataWriter, dataStream.Length); dataWriter.Write(dataStream.GetBuffer().AsSpan(0, checked ((int)dataStream.Length))); } }
public void Flush_CorrectBytesAreSaved(int shadowBufferSize, int totBytes, int writeBlockLength, bool delayedWriter) { byte[] inputBuffer = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray(); byte[] destBuffer = new byte[totBytes]; MemoryStream destStream = new MemoryStream(destBuffer); BufferedStreamWriter s = new BufferedStreamWriter(destStream, GetConfig(shadowBufferSize, delayedWriter)); for (int ix = 0; ix < totBytes; ix += writeBlockLength) { s.Write(inputBuffer, ix, Math.Min(totBytes - ix, writeBlockLength)); } s.Flush(); Assert.AreEqual(inputBuffer, destBuffer); }
public void Flush_StreamFlushes(bool delayedWriter) { byte[] inputBuffer = Enumerable.Range(1, 10).Select(f => (byte)f).ToArray(); var streamMock = new Mock <Stream>(MockBehavior.Strict); streamMock .Setup(p => p.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())); streamMock .Setup(p => p.Flush()); BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(4, delayedWriter)); s.Write(inputBuffer, 0, inputBuffer.Length); s.Flush(); streamMock.Verify(f => f.Flush(), Times.Once()); }
public void SendCommand(byte presentationContextID, ICommand command, Action <Stream> dataSetWriter) { if (command is IMayHaveDataSet mayHaveDataSet) { if ((dataSetWriter == null) && mayHaveDataSet.IsDataSetRequired()) { throw new ArgumentException($"{command} must be followed by a data set"); } } else if (dataSetWriter != null) { throw new ArgumentException($"{command} must not be followed by a data set"); } var commandAttribute = command.GetType().GetCustomAttribute <CommandAttribute>(); command.CommandField = commandAttribute.CommandType; command.CommandDataSetType = (ushort)((dataSetWriter == null) ? 0x0101 : 0xFEFE); if (TraceWriter != null) { NetUtils.TraceOutput(TraceWriter, $"PC {presentationContextID} sending ", command); } using (var stream = new PresentationContextOutputStream(this, presentationContextID, FragmentType.Command)) { var output = new BufferedStreamWriter(stream); CommandSerialization.WriteTo(output, command); output.Flush(FlushMode.Deep); } if (dataSetWriter != null) { using (var stream = new PresentationContextOutputStream(this, presentationContextID, FragmentType.DataSet)) { dataSetWriter.Invoke(stream); stream.Flush(); } } }
/// <summary>Writes a DICOM file header to a stream</summary> public static void WriteHeader(BufferedStreamWriter output, DicomFileMetaInformation fileMetaInformation) { output.WriteZeros(128); output.WriteBytes(BeforeFileMetaInformationLength); var fileMetaInformationLengthPosition = output.Position; output.WriteZeros(4); output.WriteBytes(AfterFileMetaInformationLength); var metaInformationWriter = DicomStreamWriter.Create(output, DicomUID.TransferSyntax.ExplicitVRLittleEndian); FileMetaInformationSerializer.Serialize(metaInformationWriter, fileMetaInformation); var endOfFileMetaInformationPosition = output.Position; output.Flush(FlushMode.Shallow); var fileMetaInformationLength = endOfFileMetaInformationPosition - (fileMetaInformationLengthPosition + 4); output.Stream.Seek(fileMetaInformationLengthPosition, SeekOrigin.Begin); output.Stream.Write(BitConverter.GetBytes((uint)fileMetaInformationLength)); output.Stream.Seek(endOfFileMetaInformationPosition, SeekOrigin.Begin); }