public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (IsReadFinishedBytes) { return(-1); } if (count + Position > Length) { IsReadFinishedBytes = true; //Console.WriteLine("length:" + Length + "pos:" + Position); //Console.WriteLine("need take:" + (Length - Position + BoundarySize)); byte[] endBuffer = await SignalGoStreamBase.CurrentBase.ReadBlockSizeAsync(CurrentStream, (int)(Length - Position + BoundarySize)); //Console.WriteLine("sizeTake:" + endBuffer.Length); if (endBuffer.Length == 0) { return(0); } int needRead = (int)BoundarySize; //Console.WriteLine(endBuffer.Length + "&" + (endBuffer.Length - needRead) + " & " + needRead); string text = Encoding.UTF8.GetString(endBuffer.ToList().GetRange(endBuffer.Length - needRead, needRead).ToArray()); int lineLen = 0; if (!text.StartsWith(TextHelper.NewLine)) { lineLen = 2; _Length -= 2; } //Console.WriteLine("ok&" + (endBuffer.Length - needRead - lineLen)); List <byte> newBuffer = endBuffer.ToList().GetRange(0, endBuffer.Length - needRead - lineLen); if (newBuffer.Count == 0) { return(-1); } for (int i = 0; i < newBuffer.Count; i++) { buffer[i] = newBuffer[i]; } return(newBuffer.Count); } if (count + Position > Length) { count = (int)(Length - Position); if (count <= 0) { FinishRead(); return(-1); } } byte[] readedBuffer = new byte[count]; int readCount = await CurrentStream.ReadAsync(readedBuffer, count); Array.Copy(readedBuffer, buffer, readCount); Position += readCount; if (Position == Length) { FinishRead(); } return(readCount); }
public override void BeginArrayEntry() { if (FinishedEntry) { CurrentStream.Write(Constants.ArrayEntrySeparator); } this.FinishedEntry = false; }
/// <summary> /// Draws the text in the provided font at the specified position and returns the letters which will be drawn. /// </summary> /// <param name="text">The text to draw to the page.</param> /// <param name="fontSize">The size of the font in user space units.</param> /// <param name="position">The position of the baseline (lower-left corner) to start drawing the text from.</param> /// <param name="font"> /// A font added to the document using <see cref="PdfDocumentBuilder.AddTrueTypeFont"/> /// or <see cref="PdfDocumentBuilder.AddStandard14Font"/> methods. /// </param> /// <returns>The letters from the input text with their corresponding size and position.</returns> public IReadOnlyList <Letter> AddText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font) { if (font == null) { throw new ArgumentNullException(nameof(font)); } if (text == null) { throw new ArgumentNullException(nameof(text)); } if (!documentBuilder.Fonts.TryGetValue(font.Id, out var fontStore)) { throw new ArgumentException($"No font has been added to the PdfDocumentBuilder with Id: {font.Id}. " + $"Use {nameof(documentBuilder.AddTrueTypeFont)} to register a font.", nameof(font)); } if (fontSize <= 0) { throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than 0"); } var fontProgram = fontStore.FontProgram; var fm = fontProgram.GetFontMatrix(); var textMatrix = TransformationMatrix.FromValues(1, 0, 0, 1, position.X, position.Y); var letters = DrawLetters(text, fontProgram, fm, fontSize, textMatrix); CurrentStream.Add(BeginText.Value); CurrentStream.Add(new SetFontAndSize(font.Name, fontSize)); CurrentStream.Add(new MoveToNextLineWithOffset((decimal)position.X, (decimal)position.Y)); var bytesPerShow = new List <byte>(); foreach (var letter in text) { if (char.IsWhiteSpace(letter)) { CurrentStream.Add(new ShowText(bytesPerShow.ToArray())); bytesPerShow.Clear(); } var b = fontProgram.GetValueForCharacter(letter); bytesPerShow.Add(b); } if (bytesPerShow.Count > 0) { CurrentStream.Add(new ShowText(bytesPerShow.ToArray())); } CurrentStream.Add(EndText.Value); return(letters); }
public void TemporaryShutdown() { if (CurrentSourceVoice == null) { throw new Exception(); // TEMP } savedSamplesPlayed = (int)CurrentSourceVoice.SamplesPlayed; CurrentSourceVoice.Stop(); CurrentStream.Dispose(); }
public void ShutdownRevival() { CurrentStream.Load(savedSamplesPlayed); savedSamplesPlayed = 0; if (CurrentChannel != null) { CurrentChannel.ReapplySettings(); } }
public override int Read(byte[] buffer, int offset, int count) { int readBytes; do { readBytes = CurrentStream.Read(buffer, offset, count); }while (readBytes == 0 && _streamIndex < _streams.Length - 1); return(readBytes); }
public override void BeginDictionaryEntry(string name) { if (FinishedEntry) { CurrentStream.Write(Constants.DictionaryNameValueSeparator); } FinishedEntry = false; CurrentStream.Write(Constants.StartName); CurrentStream.Write(ValidateName(name)); CurrentStream.Write(Constants.WhiteSpace); }
/// <summary> /// Writes a boolean value to the current stream /// </summary> /// <param name="value"></param> public override void WriteBoolean(bool value) { if (value) { CurrentStream.Write("true"); } else { CurrentStream.Write("false"); } }
/// <summary> /// Adds the JPEG image represented by the input stream at the specified location. /// </summary> public AddedImage AddJpeg(Stream fileStream, PdfRectangle placementRectangle) { var startFrom = fileStream.Position; var info = JpegHandler.GetInformation(fileStream); byte[] data; using (var memory = new MemoryStream()) { fileStream.Seek(startFrom, SeekOrigin.Begin); fileStream.CopyTo(memory); data = memory.ToArray(); } var imgDictionary = new Dictionary <NameToken, IToken> { { NameToken.Type, NameToken.Xobject }, { NameToken.Subtype, NameToken.Image }, { NameToken.Width, new NumericToken(info.Width) }, { NameToken.Height, new NumericToken(info.Height) }, { NameToken.BitsPerComponent, new NumericToken(info.BitsPerComponent) }, { NameToken.ColorSpace, NameToken.Devicergb }, { NameToken.Filter, NameToken.DctDecode }, { NameToken.Length, new NumericToken(data.Length) } }; var reference = documentBuilder.AddImage(new DictionaryToken(imgDictionary), data); if (!resourcesDictionary.TryGetValue(NameToken.Xobject, out var xobjectsDict) || !(xobjectsDict is DictionaryToken xobjects)) { xobjects = new DictionaryToken(new Dictionary <NameToken, IToken>()); resourcesDictionary[NameToken.Xobject] = xobjects; } var key = NameToken.Create($"I{imageKey++}"); resourcesDictionary[NameToken.Xobject] = xobjects.With(key, new IndirectReferenceToken(reference)); CurrentStream.Add(Push.Value); // This needs to be the placement rectangle. CurrentStream.Add(new ModifyCurrentTransformationMatrix(new [] { (decimal)placementRectangle.Width, 0, 0, (decimal)placementRectangle.Height, (decimal)placementRectangle.BottomLeft.X, (decimal)placementRectangle.BottomLeft.Y })); CurrentStream.Add(new InvokeNamedXObject(key)); CurrentStream.Add(Pop.Value); return(new AddedImage(reference, info.Width, info.Height)); }
} // Unused, but required to handle a video stream. private void WriteStreamToFile() { string directory = this.FileDirectory.TrimEnd('/'); string filename = DateTime.Now.ToString(this.FileDateTimeFormat); string pathname = $"{directory}/{filename}.{this.FileExtension}"; using (var fs = new FileStream(pathname, FileMode.Create, FileAccess.Write)) { CurrentStream.WriteTo(fs); } this.MostRecentFilename = filename; this.MostRecentPathname = pathname; }
public DownloadContext(string FilePath) { FileInfo = new FileInfo(FilePath); if (FileInfo.Exists) { CurrentStream = FileInfo.OpenWrite(); StartBytes = CurrentStream.Length; CurrentStream.Seek(StartBytes, SeekOrigin.Current); } else { CurrentStream = FileInfo.Create(); } }
/// <summary> /// Selects a track without playing it. If the stream is already selected, the player carries on. /// </summary> /// <param name="stream">The track to select.</param> public void Select(WaveStream stream) { if (CurrentStream != stream) { CurrentOut?.Dispose(); CurrentStream?.Dispose(); CurrentOut = new WaveOut(WaveCallbackInfo.FunctionCallback()); // using the function callback makes playback independent of GUI thread, apparently CurrentOut.PlaybackStopped += PlaybackStopped; CurrentOut.Volume = _Volume; CurrentStream = stream; CurrentOut.Init(stream); StreamSource = null; } }
/// <summary> /// Draws a line on the current page between two points with the specified line width. /// </summary> /// <param name="from">The first point on the line.</param> /// <param name="to">The last point on the line.</param> /// <param name="lineWidth">The width of the line in user space units.</param> public void DrawLine(PdfPoint from, PdfPoint to, decimal lineWidth = 1) { if (lineWidth != 1) { CurrentStream.Add(new SetLineWidth(lineWidth)); } CurrentStream.Add(new BeginNewSubpath((decimal)from.X, (decimal)from.Y)); CurrentStream.Add(new AppendStraightLineSegment((decimal)to.X, (decimal)to.Y)); CurrentStream.Add(StrokePath.Value); if (lineWidth != 1) { CurrentStream.Add(new SetLineWidth(1)); } }
/// <summary> /// Creates a buffer with the remaining chunk info. /// </summary> /// <returns>Never returns null.</returns> public byte[] GetRemainingCurrentChunkBuffer() { var chunk = Context.ChunkStack.CurrentChunk; var stream = chunk.DataStream; var length = (int)(chunk.DataLength - stream.Position); var buffer = new byte[length]; var read = CurrentStream.Read(buffer, 0, length); if (length != read) { throw new EndOfStreamException(); } return(buffer); }
protected override void SetFileStream(string path, bool newFile, long fileSizeInBytes, long startingPosition) { const int bufferSize = 2 << 11; CurrentStream = UnbufferedStream.Get( path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, bufferSize); CurrentStream.Seek(startingPosition, SeekOrigin.Current); if (!newFile) { return; } CurrentStream.SetLength(fileSizeInBytes); Flush(); }
public override void WriteStringLiteral(string value, Scryber.FontEncoding encoding) { if (UseHex && encoding != FontEncoding.PDFDocEncoding) { //TODO Write encoding prefix CurrentStream.Write(Constants.StartHexString); value = ToHex(value, encoding); CurrentStream.Write(value); CurrentStream.Write(Constants.EndHexString); } else { Scryber.Text.PDFEncoding enc = Scryber.Text.PDFEncoding.GetEncoding(encoding); CurrentStream.Write(Constants.StartString); if (null != enc.Prefix && enc.Prefix.Length > 0) { CurrentStream.Write(enc.Prefix); } CurrentStream.Write(enc.GetBytes(value)); CurrentStream.Write(Constants.EndString); } }
/// <summary> /// Draws a rectangle on the current page starting at the specified point with the given width, height and line width. /// </summary> /// <param name="position">The position of the rectangle, for positive width and height this is the bottom-left corner.</param> /// <param name="width">The width of the rectangle.</param> /// <param name="height">The height of the rectangle.</param> /// <param name="lineWidth">The width of the line border of the rectangle.</param> /// <param name="fill">Whether to fill with the color set by <see cref="SetTextAndFillColor"/>.</param> public void DrawRectangle(PdfPoint position, decimal width, decimal height, decimal lineWidth = 1, bool fill = false) { if (lineWidth != 1) { CurrentStream.Add(new SetLineWidth(lineWidth)); } CurrentStream.Add(new AppendRectangle((decimal)position.X, (decimal)position.Y, width, height)); if (fill) { CurrentStream.Add(FillPathEvenOddRuleAndStroke.Value); } else { CurrentStream.Add(StrokePath.Value); } if (lineWidth != 1) { CurrentStream.Add(new SetLineWidth(lineWidth)); } }
/// <summary> /// Adds the image previously added using <see cref="AddJpeg(byte[], PdfRectangle)"/> /// or <see cref="AddPng(byte[], PdfRectangle)"/> sharing the same image to prevent duplication. /// </summary> public void AddImage(AddedImage image, PdfRectangle placementRectangle) { if (!resourcesDictionary.TryGetValue(NameToken.Xobject, out var xobjectsDict) || !(xobjectsDict is DictionaryToken xobjects)) { xobjects = new DictionaryToken(new Dictionary <NameToken, IToken>()); resourcesDictionary[NameToken.Xobject] = xobjects; } var key = NameToken.Create($"I{imageKey++}"); resourcesDictionary[NameToken.Xobject] = xobjects.With(key, new IndirectReferenceToken(image.Reference)); CurrentStream.Add(Push.Value); // This needs to be the placement rectangle. CurrentStream.Add(new ModifyCurrentTransformationMatrix(new[] { (decimal)placementRectangle.Width, 0, 0, (decimal)placementRectangle.Height, (decimal)placementRectangle.BottomLeft.X, (decimal)placementRectangle.BottomLeft.Y })); CurrentStream.Add(new InvokeNamedXObject(key)); CurrentStream.Add(Pop.Value); }
public void Write(byte value) { CurrentStream.WriteByte(value); }
public void Write(byte[] buffer, int offset, int count) { CurrentStream.Write(buffer, offset, count); }
public override void Flush() { CurrentStream.Flush(); }
/// <summary> /// Releases the underlying stream. /// </summary> public override void Dispose() { CurrentStream?.Dispose(); }
private void NewVolumeStream() { CurrentStream++; string newVolumeName = null; int p = _archiveName.LastIndexOf("."); if (p >= 0 && _altNames && (p + 1) == _archiveName.Length - 3) { if (CurrentStream == 0) { newVolumeName = _archiveName; } else { newVolumeName = _archiveName.Substring(0, p + 1) + (CurrentStream > 9 ? (CurrentStream > 99 ? CurrentStream.ToString() : "0" + CurrentStream) : "00" + CurrentStream); } } else { newVolumeName = _archiveName + VolumeNumber(CurrentStream + 1); } Streams.Add(File.Create(newVolumeName)); Streams[CurrentStream].SetLength(_volumeSize); StreamOffsets.Add(CurrentStream, new KeyValuePair <long, long>(0, _volumeSize - 1)); }
public byte[] ToBytes() { return(CurrentStream.ToArray()); }
public void Write(byte[] buffer) { CurrentStream.Write(buffer, 0, buffer.Length); }
/// <summary> /// Writes a long value to the current stream /// </summary> /// <param name="value"></param> public override void WriteNumber(long value) { CurrentStream.Write(value.ToString()); }
protected override void DisposeCode() { CurrentStream.Dispose(); }
/// <summary> /// Writes a real value to the current stream /// </summary> /// <param name="value"></param> public override void WriteReal(float value) { CurrentStream.Write(value.ToString("F", System.Globalization.CultureInfo.InvariantCulture)); }
public void Dispose() { CurrentStream?.Dispose(); }
/// <summary> /// Writes a PDFName string to the current stream /// </summary> /// <param name="name"></param> public override void WriteName(string name) { CurrentStream.Write(Constants.StartName); CurrentStream.Write(ValidateName(name)); }