private bool Write(ChunkedStream outStream, WireMessage[] messages, bool flush = true) { ThrowIfDisposed(); if (outStream == null) { return(false); } using (var dataStream = new ChunkedStream()) { try { var dataSize = (int)_serializer.Serialize(messages, dataStream); if (dataSize > RpcMessageSizeOf.MaxAllowedData) { return(false); } /* Header */ WriteHeader(outStream, dataSize, out byte[] messageId); if (dataSize == 0) { return(true); } dataStream.Position = 0; outStream.ReadFrom(dataStream, dataSize); /* var buffer = ByteArrayCache.Default.Acquire(); * try * { * var bufferLen = buffer.Length; * * using (var dataReader = dataStream.NewReader(0)) * { * var readLen = 0; * while (dataSize > 0) * { * readLen = dataReader.Read(buffer, 0, bufferLen); * * readLen = Math.Max(0, readLen); * if (readLen > 0) * { * dataSize -= readLen; * bufferLen = Math.Min(bufferLen, dataSize); * * outStream.Write(buffer, 0, readLen); * } * } * } * } * finally * { * ByteArrayCache.Default.Release(buffer); * } */ } finally { if (flush) { outStream.Flush(); } } } return(true); }