/// <summary> /// Reads the whole document as a byte stream /// </summary> public static Stream StreamAllRawText() { var doc = PluginBase.CurrentScintillaGateway; var length = doc.GetLength(); int gap = doc.GetGapPosition(); Debug.WriteLine($"ScintillaStreams StreamAllRawText: gap at {gap}/{length}"); if (length == gap) { var characterPointer = doc.GetCharacterPointer(); unsafe { return(new UnmanagedMemoryStream((byte *)characterPointer.ToPointer(), length, length, FileAccess.Read)); } } return(ConcatenatingStream.FromFactoryFunctions(false, () => { var rangePtr = doc.GetRangePointer(0, gap); unsafe { return new UnmanagedMemoryStream((byte *)rangePtr.ToPointer(), gap, gap, FileAccess.Read); } }, () => { var rangePtr = doc.GetRangePointer(gap + 1, length); unsafe { return new UnmanagedMemoryStream((byte *)rangePtr.ToPointer(), length - gap, length - gap, FileAccess.Read); } })); }
public void Meh2() { var buffer1 = new byte[128]; var buffer2 = new byte[128]; for (byte i = 0; i < 128; i++) { buffer1[i] = i; buffer2[i] = (byte)(i + 128); } var stream1 = new MemoryStream(buffer1); var stream2 = new MemoryStream(buffer2); var c = new ConcatenatingStream(new Stream[] { stream1, stream2 }, true); var resultBuffer = new byte[256]; var data = c.Read(resultBuffer, 0, 256); Assert.AreEqual(256, data); resultBuffer.AssertEqualData(0, buffer1, 0, 128); resultBuffer.AssertEqualData(128, buffer2, 0, 128); }
public void Meh() { var r = new Random(); var buffer1 = new byte[1024]; r.NextBytes(buffer1); var stream1 = new MemoryStream(buffer1); var buffer2 = new byte[1024]; r.NextBytes(buffer2); var stream2 = new MemoryStream(buffer2); var c = new ConcatenatingStream(new Stream[] { stream1, stream2 }, true); var resultBuffer = new byte[2048]; var data = c.Read(resultBuffer, 0, 2048); Assert.AreEqual(2048, data); resultBuffer.AssertEqualData(0, buffer1, 0, 1024); resultBuffer.AssertEqualData(1024, buffer2, 0, 1024); }
/// <summary> /// Reads the whole document as a byte stream. /// Will likely throw exceptions if the document is edited while the stream is open. /// </summary> public static Stream StreamAllRawText() { var doc = PluginBase.CurrentScintillaGateway; var length = doc.GetLength(); // When editing a document Scintilla divides it into two - one before the cursor and one after, calling the break point the "gap" int gap = doc.GetGapPosition(); Debug.WriteLine($"ScintillaStreams StreamAllRawText: gap at {gap}/{length}"); // If not currently editing there is no "gap", and the gap is the end of the document if (length == gap) { var characterPointer = doc.GetCharacterPointer(); unsafe { return(new UnmanagedMemoryStream((byte *)characterPointer.ToPointer(), length, length, FileAccess.Read)); } } // If there is a gap we create two streams - one for the first part and one for the second, and concatenate them return(ConcatenatingStream.FromFactoryFunctions(false, () => { var rangePtr = doc.GetRangePointer(0, gap); unsafe { return new UnmanagedMemoryStream((byte *)rangePtr.ToPointer(), gap, gap, FileAccess.Read); } }, () => { var rangePtr = doc.GetRangePointer(gap + 1, length); unsafe { return new UnmanagedMemoryStream((byte *)rangePtr.ToPointer(), length - gap, length - gap, FileAccess.Read); } })); }