Exemplo n.º 1
0
 /// <summary>
 /// Flushes the underlying stream if an edit was performed (i.e., difference was detected).
 /// </summary>
 public override void Flush()
 {
     if (DifferenceDetected)
     {
         BaseStream.Flush();
     }
 }
Exemplo n.º 2
0
        private void Compile(bool end)
        {
            if (IsDisposed)
            {
                return;
            }

            WriteStart();

            if (CurrentNode.Count > 0)
            {
                var json = Json.Encode(CurrentNode);

                if (_ObjectID++ > 0)
                {
                    json = ", " + json;
                }

                var buffer = Encoding.GetBytes(json);

                BaseStream.Write(buffer, 0, buffer.Length);

                CurrentNode.Clear();
            }

            if (end)
            {
                WriteEnd();
            }

            BaseStream.Flush();
        }
Exemplo n.º 3
0
        public void SendMessage(int messageType, string message)
        {
            byte[] messageArray = Encoding.UTF8.GetBytes(message);
            int    dataLength   = messageArray.Length + 16;

            MemoryStream buffer = new MemoryStream(dataLength);

            // Data length (4)
            buffer.Write(ToBE(BitConverter.GetBytes(dataLength)), 0, 4);
            // Header length and Data type (4)
            buffer.Write(new byte[] { 0x00, 0x10, 0x00, 0x01 }, 0, 4);
            // Message type (4)
            buffer.Write(ToBE(BitConverter.GetBytes(messageType)), 0, 4);
            // Split (4)
            buffer.Write(ToBE(BitConverter.GetBytes(1)), 0, 4);
            // Message
            buffer.Write(messageArray, 0, messageArray.Length);

            BaseStream.Write(buffer.GetBuffer(), 0, dataLength);
            BaseStream.Flush();

            if (BaseWebSocket != null)
            {
                byte[] b = new byte[dataLength];
                BaseStream.Position = 0;
                BaseStream.Read(b, 0, dataLength);
                BaseWebSocket.SendAsync(new ArraySegment <byte>(b), WebSocketMessageType.Binary, true, CancellationToken.None).GetAwaiter().GetResult();
                BaseStream.Position = 0;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Clears all buffers for this stream and causes any buffered data to be written
        /// to the underlying device.
        /// </summary>
        /// <remarks>
        /// Flushes the <see cref="BaseStream"/>.
        /// </remarks>
        /// <exception cref="System.ObjectDisposedException">
        /// The stream has been disposed.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// The stream does not support writing.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public override void Flush()
        {
            CheckDisposed();
            CheckCanWrite();

            BaseStream.Flush();
        }
Exemplo n.º 5
0
 public override void Flush()
 {
     if (BaseStream != null)
     {
         BaseStream.Flush();
     }
 }
Exemplo n.º 6
0
 public void Flush()
 {
     lock (Locker)
     {
         BaseStream.Flush();
     }
 }
Exemplo n.º 7
0
 public override void Flush()
 {
     lock (Locker)
     {
         BaseStream.Flush();
     }
 }
Exemplo n.º 8
0
        public override void Write(MessageDelivery delivery)
        {
            Message message = Converter.ToMessage(delivery);

            Encoder.WriteMessage(message, BaseStream);
            BaseStream.Flush();
        }
Exemplo n.º 9
0
        /// <summary> Shrink file to the new size. </summary>
        /// <param name="newCount">Number of items the file should contain after this operation</param>
        protected void PerformTruncateFile(long newCount)
        {
            ThrowOnNotInitialized();
            long fileCount = GetCount();

            if (newCount < 0 || newCount > fileCount)
            {
                throw new ArgumentOutOfRangeException("newCount", newCount, "Must be >= 0 and <= Count");
            }

            // Optimize empty requests
            if (fileCount == newCount)
            {
                return;
            }

            BaseStream.SetLength(ItemIdxToOffset(newCount));
            BaseStream.Flush();

            // Just in case, hope this will never happen
            fileCount = GetCount();
            if (newCount != fileCount)
            {
                throw new BinaryFileException(
                          "Internal error: the new file should have had {0} items, but was calculated to have {1}",
                          newCount, fileCount);
            }
        }
Exemplo n.º 10
0
 public override void Flush()
 {
     BaseStream.Flush();
     foreach (var writer in CopyWriters)
     {
         writer.Flush();
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// This method is not proxied to the underlying stream; instead, the wrapper
 /// is marked as unusable for other (non-close/Dispose) operations. The underlying
 /// stream is flushed if the wrapper wasn't closed before this call.
 /// </summary>
 public override void Close()
 {
     if (!closed)
     {
         BaseStream.Flush();
     }
     closed = true;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Flushes the underlying stream.
 /// </summary>
 public void Flush()
 {
     if (BaseStream == null)
     {
         throw new NullReferenceException("Base stream is null");
     }
     BaseStream.Flush();
 }
Exemplo n.º 13
0
 public void Dispose()
 {
     BaseStream.Dispose();
     BaseStream.Flush();
     m_writer.Flush();
     m_writer.Dispose();
     m_writer = null;
 }
Exemplo n.º 14
0
 protected override Result FlushImpl()
 {
     lock (Locker)
     {
         BaseStream.Flush();
         return(Result.Success);
     }
 }
        /// <summary>
        /// Flushes the underlying stream.
        /// </summary>
        public void Flush()
        {
            if (BaseStream is null)
            {
                throw new NullReferenceException(Properties.Resources.BaseStreamCurrentlyNullError);
            }

            BaseStream.Flush();
        }
Exemplo n.º 16
0
        /// <summary>
        /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
        /// </summary>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Flush()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            BaseStream.Flush();
        }
Exemplo n.º 17
0
        /// <summary>
        /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device
        /// </summary>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Flush()
        {
            if (_closed)
            {
                throw new InvalidOperationException();
            }

            BaseStream.Flush();
        }
Exemplo n.º 18
0
 /// <inheritdoc cref="SerialPort.Close"/>
 public new void Close()
 {
     if (IsOpen)
     {
         BaseStream.Flush();
         DiscardInBuffer();
         base.Close();
     }
 }
Exemplo n.º 19
0
        public void Negotiate()
        {
            RSAKeys rsaKeys = new RSAKeys();

            BaseStream.Write(rsaKeys.PublicKey);
            RSAParameters localPrivateKey = rsaKeys.PrivateKey;

            BaseStream.Flush();
            RSAParameters remotePublicKey = BaseStream.ReadRSAParameters();

            Random rnd     = Processor.CreateRandom();
            int    nbTries = 0;

            byte[] id;
            do
            {
                if (nbTries > 100)
                {
                    throw new IOException("Can't find a different ID to use.");
                }

                this.LocalID = rnd.Next();
                id           = this.LocalID.GetBytes();

                BaseStream.WriteWrapped(RSA.Encrypt(id, remotePublicKey));
                this.RemoteID = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey).ToInt32();

                ++nbTries;
            } while(this.LocalID == this.RemoteID);

            byte[] key;
            byte[] iv;

            if (this.RemoteID < LocalID)
            {
                AES.ProduceKeyIV(out key, out iv);

                BaseStream.WriteWrapped(RSA.Encrypt(key, remotePublicKey));
                BaseStream.WriteWrapped(RSA.Encrypt(iv, remotePublicKey));
                BaseStream.Flush();
            }
            else
            {
                key = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey);
                iv  = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey);
            }

            this.EncryptedStream = new OverAESStream(BaseStream, key, iv, NbIterations);

            this.WriteWrapped(id);
            this.Flush();

            if (this.ReadWrapped().ToInt32() != RemoteID)
            {
                throw new IOException("Can't connect to remote point.");
            }
        }
Exemplo n.º 20
0
 public void Flush()
 {
     lock (streamWriterLock)
     {
         if (BaseStream != null)
         {
             BaseStream.Flush();
         }
     }
 }
Exemplo n.º 21
0
        /// <summary>
        /// Write segment stream to internal stream, optionally truncating the file so that <paramref name="firstItemIdx"/> would be the first written item.
        /// </summary>
        /// <param name="streamEnmr">The stream of array segments to write, with a single MoveNext() already performed (returned true)</param>
        /// <param name="firstItemIdx">The index of the first element in the stream. The file will be truncated if the value is less than or equal to Count</param>
        protected void PerformWriteStreaming(
            [NotNull] IEnumerator <ArraySegment <T> > streamEnmr, long firstItemIdx = long.MaxValue)
        {
            if (streamEnmr == null)
            {
                throw new ArgumentNullException("streamEnmr");
            }

            ThrowOnNotInitialized();
            if (firstItemIdx < long.MaxValue)
            {
                PerformTruncateFile(firstItemIdx);
            }

            bool canSeek = BaseStream.CanSeek;
            long fileSize = 0, itemIdx = 0;

            if (canSeek)
            {
                fileSize = BaseStream.Length;
                bool isAligned;
                itemIdx = CalculateItemCountFromFilePosition(fileSize, out isAligned);
            }

            bool?useMemMappedAccess = null;

            // Have to call Count on every access
            do
            {
                ArraySegment <T> seg = streamEnmr.Current;
                if (seg.Count == 0)
                {
                    continue;
                }

                if (useMemMappedAccess == null)
                {
                    useMemMappedAccess = UseMemoryMappedAccess(seg.Count, true);
                }

                int read = PerformUnsafeBlockAccess(itemIdx, true, seg, fileSize, useMemMappedAccess.Value);

                if (canSeek)
                {
                    itemIdx  += read;
                    fileSize += read * ItemSize;
                }
            } while (streamEnmr.MoveNext());

            if (useMemMappedAccess == false)
            {
                BaseStream.Flush();
            }
        }
Exemplo n.º 22
0
 /// <summary>
 /// Flushes the stream
 /// </summary>
 public override void Flush()
 {
     if (!IsConnected)
     {
         throw new InvalidOperationException("The FtpSocketStream object is not connected.");
     }
     if (BaseStream == null)
     {
         throw new InvalidOperationException("The base stream of the FtpSocketStream object is null.");
     }
     BaseStream.Flush();
 }
Exemplo n.º 23
0
 public override void Flush()
 {
     if (isAdjacent)
     {
         var transformed =
             encoding.GetBytes(responseTransformer.Transform(encoding.GetString(transformBuffer.ToArray())));
         BaseStream.Write(transformed, 0, transformed.Length);
         transformBuffer.Clear();
     }
     BaseStream.Flush();
     RRTracer.Trace("Flushing Filter");
 }
Exemplo n.º 24
0
        /// <inheritdoc cref="SerialPort.Close"/>
        public new void Close()
        {
            if (!IsOpen)
            {
                return;
            }

            Thread.Sleep(250);
            BaseStream.Flush();
            DiscardInBuffer();
            BaseStream.Close();
            base.Close();
        }
Exemplo n.º 25
0
        /// <summary>
        /// Write a raw network message
        /// </summary>
        /// <param name="buffer">byte buffer</param>
        protected void WriteBytes(byte[] buffer)
        {
            int length = buffer.Length;

            if (length > UInt16.MaxValue)
            {
                throw new InvalidOperationException("Message is too long");
            }

            // write message length
            BaseStream.Write(new byte[] { (byte)(length / 256), (byte)(length & 255) }, 0, 2);

            // write message
            BaseStream.Write(buffer, 0, length);
            BaseStream.Flush();
        }
Exemplo n.º 26
0
        public void Write(IDataChunk chunk)
        {
            if (chunk is Packet packet)
            {
                Write(packet);
            }
            else if (chunk is MessageData message)
            {
                Write(message);
            }
            else
            {
                throw new InvalidOperationException("Unsupported data type: " + chunk.GetType());
            }

            BaseStream.Flush();
        }
Exemplo n.º 27
0
        /// <summary>
        /// Sends all the buffered data.
        /// </summary>
        public override void Flush()
        {
            if (fWriteBuffer != null)
            {
                if (fWriteBuffer.Length > 0)
                {
                    var encryptedBuffer = Encryptor.TransformFinalBlock(fWriteBuffer.GetBuffer(), 0, (int)fWriteBuffer.Length);
                    var size            = BitConverter.GetBytes(encryptedBuffer.Length);
                    BaseStream.Write(size, 0, size.Length);
                    BaseStream.Write(encryptedBuffer, 0, encryptedBuffer.Length);
                    BaseStream.Flush();

                    fWriteBuffer.SetLength(0);
                    fWriteBuffer.Capacity = 32 * 1024;
                }
            }
        }
Exemplo n.º 28
0
        /// <inheritdoc />
        public override unsafe void Flush()
        {
            if (_mode == Mode.Decompress)
            {
                BaseStream.Flush();
                return;
            }

            fixed(byte *writePtr = _workBuf)
            {
                _lzmaStream.NextIn   = (byte *)0;
                _lzmaStream.AvailIn  = 0;
                _lzmaStream.NextOut  = writePtr + _workBufPos;
                _lzmaStream.AvailOut = (uint)(_workBuf.Length - _workBufPos);

                LzmaRet ret = LzmaRet.Ok;

                while (ret != LzmaRet.StreamEnd)
                {
                    int writeSize = 0;
                    if (_lzmaStream.AvailOut != 0)
                    {
                        ulong bakAvailOut = _lzmaStream.AvailOut;
                        ret        = XZInit.Lib.LzmaCode(_lzmaStream, LzmaAction.FullFlush);
                        writeSize += (int)(bakAvailOut - _lzmaStream.AvailOut);
                    }
                    _workBufPos += writeSize;

                    BaseStream.Write(_workBuf, 0, _workBufPos);
                    TotalOut += _workBufPos;

                    // Reset NextOut and AvailOut
                    _workBufPos          = 0;
                    _lzmaStream.NextOut  = writePtr;
                    _lzmaStream.AvailOut = (uint)_workBuf.Length;

                    // Once everything has been encoded successfully, the return value of lzma_code() will be LZMA_STREAM_END.
                    if (ret != LzmaRet.Ok && ret != LzmaRet.StreamEnd)
                    {
                        throw new XZException(ret);
                    }
                }
            }

            BaseStream.Flush();
        }
Exemplo n.º 29
0
        public override void Close()
        {
            byte[] buffer = _stream.ToArray();
            if (_threshold <= 0 || _stream.Length > _threshold)
            {
                BaseStream.Write(buffer, 0, buffer.Length);
                BaseStream.Flush();
                BaseStream.Close();
            }
            else
            {
                _baseStream.Write(buffer, 0, buffer.Length);
                _baseStream.Flush();
                _baseStream.Close();
            }

            _stream.Close();
        }
Exemplo n.º 30
0
        protected internal int PerformFileAccess(long firstItemIdx, ArraySegment <T> buffer, bool isWriting)
        {
            ThrowOnNotInitialized();

            bool canSeek = BaseStream.CanSeek;

            if (!canSeek && firstItemIdx != 0)
            {
                throw new ArgumentOutOfRangeException(
                          "firstItemIdx", firstItemIdx,
                          "Must be 0 when the base stream is not seekable");
            }

            if (canSeek && (firstItemIdx < 0 || firstItemIdx > GetCount()))
            {
                throw new ArgumentOutOfRangeException("firstItemIdx", firstItemIdx, "Must be >= 0 and <= Count");
            }

            // Optimize empty requests
            if (buffer.Count == 0)
            {
                return(0);
            }

            int ret;

            if (IsFileStream &&
                ((isWriting && EnableMemMappedAccessOnWrite) || (!isWriting && EnableMemMappedAccessOnRead)) &&
                ItemIdxToOffset(buffer.Count) > MinReqSizeToUseMapView)
            {
                ret = ProcessMemoryMappedFile(firstItemIdx, buffer, isWriting, BaseStream.Length);
            }
            else
            {
                ret = ProcessStream(firstItemIdx, buffer, isWriting);
                if (isWriting)
                {
                    BaseStream.Flush();
                }
            }

            return(ret);
        }