public subArray <byte> GetDeCompress(byte[] compressData, memoryPool memoryPool = null) { if (compressData.length() > 0) { return(GetDeCompressUnsafe(compressData, 0, compressData.Length, memoryPool)); } return(default(subArray <byte>)); }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> public subArray<byte> GetCompress(byte[] data, int seek = 0, memoryPool memoryPool = null) { if (seek < 0) log.Error.Throw(log.exceptionType.IndexOutOfRange); if (data != null && data.Length != 0) { return GetCompressUnsafe(data, 0, data.Length, seek, memoryPool); } return default(subArray<byte>); }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> public subArray<byte> GetCompress(byte[] data, int startIndex, int count, int seek = 0, memoryPool memoryPool = null) { if (seek >= 0) { if (count == 0) return subArray<byte>.Unsafe(nullValue<byte>.Array, 0, 0); array.range range = new array.range(data.length(), startIndex, count); if (count == range.GetCount) return GetCompressUnsafe(data, startIndex, count, seek, memoryPool); } log.Error.Throw(log.exceptionType.IndexOutOfRange); return default(subArray<byte>); }
/// <summary> /// HTTP响应头部输出 /// </summary> /// <param name="buffer">输出数据</param> /// <param name="memoryPool">内存池</param> protected override void responseHeader(ref subArray <byte> buffer, memoryPool memoryPool) { if (responseSize == 0) { response.Push(ref response); sender.Send(dataSender.onSendType.Next, ref buffer, memoryPool); } else { sender.Send(dataSender.onSendType.ResponseBody, ref buffer, memoryPool); } }
public subArray <byte> GetCompress(byte[] data, int seek = 0, memoryPool memoryPool = null) { if (seek < 0) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } if (data != null && data.Length != 0) { return(GetCompressUnsafe(data, 0, data.Length, seek, memoryPool)); } return(default(subArray <byte>)); }
/// <summary> /// 发送数据 /// </summary> /// <param name="onSend">发送数据回调处理</param> /// <param name="buffer">发送数据缓冲区</param> /// <param name="memoryPool">发送数据缓冲区内存池</param> public void Send(onSendType onSend, ref subArray <byte> buffer, memoryPool memoryPool) { this.onSend = onSend; sendStartTime = date.nowTime.Now.AddTicks(date.SecondTicks); this.memoryPool = memoryPool; this.buffer = buffer.array; sendIndex = buffer.startIndex; sendLength = 0; sendEndIndex = sendIndex + buffer.length; send(); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="stream">数据流</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <returns>解压缩后的数据</returns> public subArray <byte> GetDeCompress(Stream stream, memoryPool memoryPool = null) { if (stream != null) { using (Stream compressStream = getDecompressStream(stream)) { return(new deCompressor { CompressStream = compressStream }.Get(memoryPool)); } } return(default(subArray <byte>)); }
/// <summary> /// 文件流写入器 /// </summary> /// <param name="fileName">文件全名</param> /// <param name="mode">打开方式</param> /// <param name="fileShare">共享访问方式</param> /// <param name="fileOption">附加选项</param> /// <param name="isLog">是否写日志</param> /// <param name="encoding">文件编码</param> public fileStreamWriter(string fileName, FileMode mode = FileMode.CreateNew, FileShare fileShare = FileShare.None, FileOptions fileOption = FileOptions.None, bool isLog = true, Encoding encoding = null) { if (string.IsNullOrEmpty(fileName)) { log.Error.Throw(log.exceptionType.Null); } FileName = fileName; this.isLog = isLog; this.fileShare = fileShare; this.fileOption = fileOption; this.encoding = encoding; memoryPool = memoryPool.GetOrCreate(bufferLength = (int)file.BytesPerCluster(fileName)); buffer = memoryPool.Get(); open(mode); flushWait = new EventWaitHandle(true, EventResetMode.ManualReset); }
///// <summary> ///// 字符串转换成字节数组 ///// </summary> ///// <param name="value">字符串</param> ///// <returns>字节数组+缓冲区入池调用</returns> //[MethodImpl((MethodImplOptions)fastCSharp.pub.MethodImplOptionsAggressiveInlining)] //internal unsafe memoryPool.pushSubArray GetBytes(string value) //{ // return GetBytes(value, this.encoding ?? fastCSharp.config.appSetting.Encoding); //} /// <summary> /// 字符串转换成字节数组 /// </summary> /// <param name="value">字符串</param> /// <param name="encoding"></param> /// <returns>字节数组+缓冲区入池调用</returns> internal static unsafe memoryPool.pushSubArray GetBytes(string value, Encoding encoding) { int length = encoding.CodePage == Encoding.Unicode.CodePage ? value.Length << 1 : encoding.GetByteCount(value); memoryPool pool = memoryPool.GetDefaultPool(length); byte[] data = pool.Get(length); if (encoding.CodePage == Encoding.Unicode.CodePage) { fixed(byte *dataFixed = data) unsafer.String.Copy(value, dataFixed); } else { encoding.GetBytes(value, 0, value.Length, data, 0); } return(new memoryPool.pushSubArray { Value = subArray <byte> .Unsafe(data, 0, length), PushPool = pool }); }
/// <summary> /// 获取压缩数据 /// </summary> /// <param name="data">数据</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <param name="seek">起始位置</param> /// <returns>压缩数据,失败返回null</returns> internal static subArray <byte> GetCompress(ref subArray <byte> data, memoryPool memoryPool = null, int seek = 0) { if (data.length > GZipSize) { subArray <byte> compressData = stream.GZip.GetCompress(data.array, data.startIndex, data.length, seek, memoryPool); if (compressData.length != 0) { if (compressData.length + GZipSize < data.length) { return(compressData); } if (memoryPool != null) { memoryPool.PushNotNull(compressData.array); } } } return(default(subArray <byte>)); }
/// <summary> /// 获取解压数据 /// </summary> /// <returns>解压数据</returns> public unsafe subArray <byte> Get(memoryPool memoryPool) { if (memoryPool == null) { pointer data = fastCSharp.unmanagedPool.StreamBuffers.Get(); try { using (dataStream = new unmanagedStream(data.Byte, fastCSharp.unmanagedPool.StreamBuffers.Size)) { get(); return(new subArray <byte>(dataStream.GetArray())); } } finally { fastCSharp.unmanagedPool.StreamBuffers.Push(ref data); } } else { byte[] data = memoryPool.Get(); try { fixed(byte *dataFixed = data) { using (dataStream = new unmanagedStream(dataFixed, data.Length)) { get(); if (dataStream.data.data == dataFixed) { byte[] buffer = data; data = null; return(subArray <byte> .Unsafe(buffer, 0, dataStream.Length)); } return(new subArray <byte>(dataStream.GetArray())); } } } finally { memoryPool.PushOnly(data); } } }
/// <summary> /// 获取解压数据 /// </summary> /// <returns>解压数据</returns> public unsafe subArray<byte> Get(memoryPool memoryPool) { if (memoryPool == null) { pointer data = fastCSharp.unmanagedPool.StreamBuffers.Get(); try { using (dataStream = new unmanagedStream(data.Byte, fastCSharp.unmanagedPool.StreamBuffers.Size)) { get(); return new subArray<byte>(dataStream.GetArray()); } } finally { fastCSharp.unmanagedPool.StreamBuffers.Push(ref data); } } else { byte[] data = memoryPool.Get(); try { fixed (byte* dataFixed = data) { using (dataStream = new unmanagedStream(dataFixed, data.Length)) { get(); if (dataStream.Data == dataFixed) { byte[] buffer = data; data = null; return subArray<byte>.Unsafe(buffer, 0, dataStream.Length); } return new subArray<byte>(dataStream.GetArray()); } } } finally { memoryPool.Push(ref data); } } }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <returns>压缩后的数据,失败返回null</returns> public subArray <byte> GetCompress(byte[] data, int startIndex, int count, int seek = 0, memoryPool memoryPool = null) { if (seek >= 0) { if (count == 0) { return(subArray <byte> .Unsafe(nullValue <byte> .Array, 0, 0)); } array.range range = new array.range(data.length(), startIndex, count); if (count == range.GetCount) { return(GetCompressUnsafe(data, startIndex, count, seek, memoryPool)); } } log.Error.Throw(log.exceptionType.IndexOutOfRange); return(default(subArray <byte>)); }
/// <summary> /// 获取缩略图 /// </summary> /// <param name="data">输出数据</param> /// <param name="width">缩略宽度</param> /// <param name="height">缩略高度</param> /// <param name="type">目标图像文件格式</param> /// <param name="backColor">背景色</param> /// <param name="memoryPool">输出数据缓冲区内存池</param> /// <param name="seek">输出数据起始位置</param> public void Pad(ref subArray <byte> data, ref int width, ref int height, ImageFormat type, Color backColor, memoryPool memoryPool, int seek) { if (checkPad(ref width, ref height)) { if (memoryPool == null) { using (MemoryStream stream = new MemoryStream()) { if (seek != 0) { stream.Seek(seek, SeekOrigin.Begin); } pad(stream, width, height, type, backColor); data.UnsafeSet(stream.GetBuffer(), seek, (int)stream.Position - seek); return; } } byte[] buffer = memoryPool.Get(); try { using (MemoryStream stream = memoryStream.UnsafeNew(buffer)) { if (seek != 0) { stream.Seek(seek, SeekOrigin.Begin); } pad(stream, width, height, type, backColor); byte[] bufferData = stream.GetBuffer(); if (buffer == bufferData) { buffer = null; //showjim if ((int)stream.Position > bufferData.Length) { log.Error.Add("Position " + ((int)stream.Position).toString() + " > " + bufferData.Length.toString(), null, false); } } data.UnsafeSet(bufferData, seek, (int)stream.Position - seek); return; } } finally { memoryPool.PushOnly(buffer); } } data.Null(); }
/// <summary> /// 写入日志 /// </summary> /// <param name="data">日志数据</param> /// <returns>是否成功写入缓冲区</returns> public unsafe bool AppendLog(memoryPool.pushSubArray data) { if (currentStep == (int) physicalOld.step.Waitting) { byte isCopy = 0, isLog = 0; if (Interlocked.CompareExchange(ref logFileLock, 1, 0) != 0) { do { Thread.Sleep(0); if (Interlocked.CompareExchange(ref logFileLock, 1, 0) == 0) break; if (isRefreshErrorLog) Thread.Sleep(1); else Thread.Sleep(0); } while (Interlocked.CompareExchange(ref logFileLock, 1, 0) != 0); } try { if (logFileWriter.UnsafeWrite(data) >= 0) return true; lastException = logFileWriter.LastException; //subArray<byte> dataArray = data.Value; //int length = logBufferIndex + dataArray.Length; //if (length <= Buffers.Size) //{ // if (logBuffer == null) logBuffer = Buffers.Get(); // fixed (byte* dataFixed = dataArray.Array, bufferFixed = logBuffer) // { // unsafer.memory.Copy(dataFixed + dataArray.StartIndex, bufferFixed + logBufferIndex, dataArray.Length); // } // logBufferIndex = length; // isCopy = isLog = 1; //} //else if (logBufferIndex == 0) //{ // if (logFileWriter.UnsafeWrite(data) >= 0) isLog = 1; // else lastException = logFileWriter.LastException; //} //else //{ // fixed (byte* dataFixed = dataArray.Array, bufferFixed = logBuffer) // { // unsafer.memory.Copy(dataFixed + dataArray.StartIndex, bufferFixed + logBufferIndex, length = Buffers.Size - logBufferIndex); // dataArray.UnsafeSet(dataArray.StartIndex + length, dataArray.Length - length); // if (logFileWriter.UnsafeWrite(new memoryPool.pushSubArray { Value = subArray<byte>.Unsafe(logBuffer, 0, Buffers.Size), PushPool = Buffers.PushHandle }) >= 0) // { // if (dataArray.Length <= Buffers.Size) // { // fixed (byte* newBufferFixed = logBuffer = Buffers.Get()) // { // unsafer.memory.Copy(dataFixed + dataArray.StartIndex, newBufferFixed, logBufferIndex = dataArray.Length); // } // isCopy = isLog = 1; // } // else if (logFileWriter.UnsafeWrite(data) >= 0) // { // logBufferIndex = 0; // logBuffer = null; // isLog = 1; // } // else lastException = logFileWriter.LastException; // } // else // { // isCopy = 1; // lastException = logFileWriter.LastException; // } // } //} } catch (Exception error) { lastException = error; } finally { logFileLock = 0; } if (isCopy != 0) data.Push(); if (isLog != 0) return true; Dispose(); } return false; }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> internal subArray<byte> GetCompressUnsafe(byte[] data, int startIndex, int count, int seek = 0, memoryPool memoryPool = null) { int length = count + seek; if (memoryPool == null) { using (MemoryStream dataStream = new MemoryStream()) { if (seek != 0) dataStream.Seek(seek, SeekOrigin.Begin); using (Stream compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { return subArray<byte>.Unsafe(dataStream.GetBuffer(), seek, (int)dataStream.Position - seek); } } } else { byte[] buffer = memoryPool.Get(); try { using (MemoryStream dataStream = memoryStream.Get(buffer)) { if (seek != 0) dataStream.Seek(seek, SeekOrigin.Begin); using (Stream compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { byte[] streamBuffer = dataStream.GetBuffer(); if (streamBuffer == buffer) buffer = null; return subArray<byte>.Unsafe(streamBuffer, seek, (int)dataStream.Position - seek); } } } catch (Exception error) { log.Error.Add(error, null, false); } finally { memoryPool.Push(ref buffer); } } return default(subArray<byte>); }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <returns>压缩后的数据,失败返回null</returns> internal subArray <byte> GetCompressUnsafe(byte[] data, int startIndex, int count, int seek = 0, memoryPool memoryPool = null) { int length = count + seek; if (memoryPool == null) { using (MemoryStream dataStream = new MemoryStream()) { if (seek != 0) { dataStream.Seek(seek, SeekOrigin.Begin); } using (Stream compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { return(subArray <byte> .Unsafe(dataStream.GetBuffer(), seek, (int)dataStream.Position - seek)); } } } else { byte[] buffer = memoryPool.Get(); try { using (MemoryStream dataStream = memoryStream.UnsafeNew(buffer)) { if (seek != 0) { dataStream.Seek(seek, SeekOrigin.Begin); } using (Stream compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { byte[] streamBuffer = dataStream.GetBuffer(); if (streamBuffer == buffer) { buffer = null; } return(subArray <byte> .Unsafe(streamBuffer, seek, (int)dataStream.Position - seek)); } } } catch (Exception error) { log.Error.Add(error, null, false); } finally { memoryPool.PushOnly(buffer); } } return(default(subArray <byte>)); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <returns>解压缩后的数据</returns> public subArray<byte> GetDeCompress(byte[] compressData, int startIndex, int count, memoryPool memoryPool = null) { if (count > 0) { array.range range = new array.range(compressData.length(), startIndex, count); if (count == range.GetCount) return GetDeCompressUnsafe(compressData, range.SkipCount, count, memoryPool); } log.Error.Throw(log.exceptionType.IndexOutOfRange); return default(subArray<byte>); }
/// <summary> /// 读取文件分块数据//showjim+cache /// </summary> /// <param name="onReaded"></param> /// <param name="index">文件分块数据位置</param> internal unsafe void Read(Func <fastCSharp.net.returnValue <fastCSharp.code.cSharp.tcpBase.subByteArrayEvent>, bool> onReaded, ref index index) { if (onReaded == null) { log.Error.Throw(log.exceptionType.Null); } long endIndex = index.EndIndex; if (index.Size > 0 && ((int)index.Index & 3) == 0 && endIndex <= fileBufferLength) { if (endIndex <= fileLength) { reader reader = reader.Get(); if (reader != null) { reader.Set(onReaded, ref index); if (set(reader)) { fastCSharp.threading.threadPool.TinyPool.FastStart(reader, thread.callType.FileBlockStreamReader); } return; } } else { memoryPool memoryPool = null; byte[] buffer = null; int copyedSize = int.MinValue; Monitor.Enter(bufferLock); if (isDisposed == 0) { if (index.Index >= bufferIndex) { index.Index -= bufferIndex; try { buffer = (memoryPool = memoryPool.GetOrCreate(index.Size)).Get(index.Size); foreach (memoryPool.pushSubArray nextData in buffers.array) { subArray <byte> data = nextData.SubArray; if (index.Index != 0) { if (index.Index >= data.length) { index.Index -= data.length; continue; } data.UnsafeSet(data.startIndex + (int)index.Index, data.length - (int)index.Index); index.Index = 0; } if (copyedSize < 0) { fixed(byte *dataFixed = data.array) { if (*(int *)(dataFixed + data.startIndex) != index.Size) { break; } } if ((copyedSize = data.length - sizeof(int)) == 0) { continue; } data.UnsafeSet(data.startIndex + sizeof(int), copyedSize); copyedSize = 0; } int copySize = index.Size - copyedSize; if (data.length >= copySize) { Buffer.BlockCopy(data.array, data.startIndex, buffer, copyedSize, copySize); copyedSize = index.Size; break; } Buffer.BlockCopy(data.array, data.startIndex, buffer, copyedSize, copySize); copyedSize += copySize; } } catch (Exception error) { log.Default.Add(error, null, false); } finally { Monitor.Exit(bufferLock); } if (copyedSize == index.Size) { onReaded(new fastCSharp.code.cSharp.tcpBase.subByteArrayEvent { Buffer = subArray <byte> .Unsafe(buffer, 0, index.Size), SerializeEvent = memoryPool }); return; } } else { Monitor.Exit(bufferLock); reader reader = reader.Get(); if (reader != null) { reader.Set(onReaded, ref index); reader.WaitFileStream = this; fastCSharp.threading.threadPool.TinyPool.FastStart(reader, thread.callType.FileBlockStreamReaderWait); return; } } } else { Monitor.Exit(bufferLock); } } } onReaded(default(fastCSharp.code.cSharp.tcpBase.subByteArrayEvent)); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="stream">数据流</param> /// <returns>解压缩后的数据</returns> public subArray<byte> GetDeCompress(Stream stream, memoryPool memoryPool = null) { if (stream != null) { using (Stream compressStream = getDecompressStream(stream)) { return new deCompressor { CompressStream = compressStream }.Get(memoryPool); } } return default(subArray<byte>); }
/// <summary> /// 开始读取文件 /// </summary> public unsafe void Read() { do { int readSize = index.Size + sizeof(int); try { if (FileStream.isDisposed == 0) { buffer = (memoryPool = memoryPool.GetPool(readSize)).Get(); FileStream fileReader = FileStream.fileReader; long offset = fileReader.Position - index.Index; if (offset >= 0 || -offset < index.Index) fileReader.Seek(offset, SeekOrigin.Current); else fileReader.Seek(index.Index, SeekOrigin.Begin); if (fileReader.Read(buffer, 0, readSize) == readSize) { fixed (byte* bufferFixed = buffer) { if (*(int*)bufferFixed == index.Size) readSize = index.Size; else log.Default.Add(FileStream.FileName + " index[" + index.Index.toString() + "] size[" + (*(int*)bufferFixed).toString() + "]<>" + index.Size.toString(), false, false); } } else readSize = 0; } } catch (Exception error) { log.Default.Add(error, null, false); } Func<fastCSharp.code.cSharp.asynchronousMethod.returnValue<fastCSharp.code.cSharp.tcpBase.subByteArrayEvent>, bool> onReaded = this.onReaded; if (readSize == index.Size) { if (onReaded(new fastCSharp.code.cSharp.tcpBase.subByteArrayEvent { Buffer = subArray<byte>.Unsafe(buffer, sizeof(int), index.Size), Event = memoryPool.PushSubArray })) buffer = null; else memoryPool.Push(ref buffer); } else { onReaded(default(fastCSharp.code.cSharp.tcpBase.subByteArrayEvent)); if (memoryPool != null) memoryPool.Push(ref buffer); } reader next = FileStream.next(this); if (next == null) { FileStream = null; onReaded = null; memoryPool = null; typePool<reader>.Push(this); return; } onReaded = next.onReaded; index = next.index; next.onReaded = null; typePool<reader>.Push(next); } while (true); }
/// <summary> /// 图像缩略切剪 /// </summary> /// <param name="data">图像文件数据</param> /// <param name="width">缩略宽度,0表示与高度同比例</param> /// <param name="height">缩略高度,0表示与宽度同比例</param> /// <param name="type">目标图像文件格式</param> /// <param name="memoryPool">输出数据缓冲区内存池</param> /// <param name="seek">输出数据起始位置</param> /// <returns>图像缩略文件数据</returns> public static void Cut(ref subArray <byte> data, int width, int height, ImageFormat type, memoryPool memoryPool = null, int seek = 0) { if (data.Count != 0 && width > 0 && height > 0 && (width | height) != 0 && seek >= 0) { try { using (MemoryStream memory = new MemoryStream(data.UnsafeArray, data.StartIndex, data.Count)) { builder builder = new builder(); using (Image image = builder.CreateImage(memory)) { builder.Cut(ref data, ref width, ref height, type, memoryPool, seek); return; } } } catch (Exception error) { log.Error.Add(error, null, false); } } data.Null(); }
public static subArray <byte> Cut(subArray <byte> data, int width, int height, ImageFormat type, memoryPool memoryPool = null, int seek = 0) { Cut(ref data, width, height, type, memoryPool, seek); return(data); }
/// <summary> /// 图像缩略切剪 /// </summary> /// <param name="data">图像文件数据</param> /// <param name="width">缩略宽度,0表示与高度同比例</param> /// <param name="height">缩略高度,0表示与宽度同比例</param> /// <param name="type">目标图像文件格式</param> /// <param name="memoryPool">输出数据缓冲区内存池</param> /// <param name="seek">输出数据起始位置</param> /// <returns>图像缩略文件数据</returns> public static subArray <byte> Cut(byte[] data, int width, int height, ImageFormat type, memoryPool memoryPool = null, int seek = 0) { if (data == null) { return(default(subArray <byte>)); } subArray <byte> dataArray = subArray <byte> .Unsafe(data, 0, data.Length); Cut(ref dataArray, width, height, type, memoryPool, seek); return(dataArray); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <returns>解压缩后的数据</returns> internal subArray <byte> GetDeCompressUnsafe(byte[] compressData, int startIndex, int count, memoryPool memoryPool) { using (Stream memoryStream = new MemoryStream(compressData, startIndex, count)) using (Stream compressStream = getDecompressStream(memoryStream)) { return(new deCompressor { CompressStream = compressStream }.Get(memoryPool)); } }
/// <summary> /// 文件流写入器 /// </summary> /// <param name="fileName">文件全名</param> /// <param name="mode">打开方式</param> /// <param name="fileShare">共享访问方式</param> /// <param name="fileOption">附加选项</param> /// <param name="encoding">文件编码</param> public fileStreamWriter(string fileName, FileMode mode = FileMode.CreateNew, FileShare fileShare = FileShare.None, FileOptions fileOption = FileOptions.None, bool isLog = true, Encoding encoding = null) { if (fileName.length() == 0) log.Error.Throw(log.exceptionType.Null); FileName = fileName; this.isLog = isLog; this.fileShare = fileShare; this.fileOption = fileOption; this.encoding = encoding; memoryPool = memoryPool.GetPool(bufferLength = (int)file.BytesPerCluster(fileName)); buffer = memoryPool.Get(); writeFileHandle = writeFile; open(mode); flushWait = new EventWaitHandle(true, EventResetMode.ManualReset); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <returns>解压缩后的数据</returns> internal subArray<byte> GetDeCompressUnsafe(byte[] compressData, int startIndex, int count, memoryPool memoryPool) { using (Stream memoryStream = new MemoryStream(compressData, startIndex, count)) using (Stream compressStream = getDecompressStream(memoryStream)) { return new deCompressor { CompressStream = compressStream }.Get(memoryPool); } }
/// <summary> /// 写入数据 /// </summary> /// <param name="data">数据</param> /// <returns>写入位置,失败返回-1</returns> internal long UnsafeWrite(memoryPool.pushSubArray data) { flushWait.Reset(); subArray<byte> dataArray = data.Value; interlocked.NoCheckCompareSetSleep0(ref bufferLock); if (isDisposed == 0) { long fileBufferLength = this.fileBufferLength; this.fileBufferLength += dataArray.Count; if (isWritting == 0) { int length = currentIndex + dataArray.Count; if (length < bufferLength && flushCount == 0) { Buffer.BlockCopy(dataArray.array, dataArray.StartIndex, buffer, currentIndex, dataArray.Count); checkFlushTime = date.NowSecond.AddTicks(checkFlushTicks); currentIndex = length; bufferIndex = this.fileBufferLength; bufferLock = 0; data.Push(); setCheckFlush(); } else { buffers.array[0] = data; buffers.Unsafer.AddLength(1); bufferSize += dataArray.Count; isFlush = 0; isWritting = 1; isCopyBuffer = 0; bufferLock = 0; threadPool.TinyPool.FastStart(writeFileHandle, null, null); } } else { try { buffers.Add(data); bufferSize += dataArray.Count; isCopyBuffer = 0; } finally { bufferLock = 0; } } return fileBufferLength; } bufferLock = 0; data.Push(); return -1; }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <param name="memoryPool">数据缓冲区内存池</param> /// <returns>解压缩后的数据</returns> public subArray <byte> GetDeCompress(byte[] compressData, int startIndex, int count, memoryPool memoryPool = null) { if (count > 0) { array.range range = new array.range(compressData.length(), startIndex, count); if (count == range.GetCount) { return(GetDeCompressUnsafe(compressData, range.SkipCount, count, memoryPool)); } } log.Error.Throw(log.exceptionType.IndexOutOfRange); return(default(subArray <byte>)); }
private void setBuffer(int bufferSize) { buffer = (memoryPool = memoryPool.GetPool(bufferSize)).Get(); BufferIndex = sizeof(int); }
/// <summary> /// 获取压缩数据 /// </summary> /// <param name="data">数据</param> /// <returns>压缩数据,失败返回null</returns> internal static subArray<byte> GetCompress(subArray<byte> data, memoryPool memoryPool = null, int seek = 0) { if (data.Count > GZipSize) { subArray<byte> compressData = stream.GZip.GetCompress(data.Array, data.StartIndex, data.Count, seek, memoryPool); if (compressData.Count != 0) { if (compressData.Count + GZipSize < data.Count) return compressData; if (memoryPool != null) memoryPool.Push(ref compressData.array); } } return default(subArray<byte>); }
public static subArray <byte> Pad(subArray <byte> data, int width, int height, ImageFormat type, Color backColor, memoryPool memoryPool = null, int seek = 0) { Pad(ref data, width, height, type, backColor, memoryPool, seek); return(data); }
/// <summary> /// 开始读取文件 /// </summary> public unsafe void Read() { do { int readSize = index.Size + sizeof(int); try { if (FileStream.isDisposed == 0) { buffer = (memoryPool = memoryPool.GetOrCreate(readSize)).Get(); FileStream fileReader = FileStream.fileReader; long offset = fileReader.Position - index.Index; if (offset >= 0 || -offset < index.Index) { fileReader.Seek(offset, SeekOrigin.Current); } else { fileReader.Seek(index.Index, SeekOrigin.Begin); } if (fileReader.Read(buffer, 0, readSize) == readSize) { fixed(byte *bufferFixed = buffer) { if (*(int *)bufferFixed == index.Size) { readSize = index.Size; } else { log.Default.Add(FileStream.FileName + " index[" + index.Index.toString() + "] size[" + (*(int *)bufferFixed).toString() + "]<>" + index.Size.toString(), new System.Diagnostics.StackFrame(), false); } } } else { readSize = 0; } } } catch (Exception error) { log.Default.Add(error, null, false); } Func <fastCSharp.net.returnValue <fastCSharp.code.cSharp.tcpBase.subByteArrayEvent>, bool> onReaded = this.onReaded; if (readSize == index.Size) { if (onReaded(new fastCSharp.code.cSharp.tcpBase.subByteArrayEvent { Buffer = subArray <byte> .Unsafe(buffer, sizeof(int), index.Size), SerializeEvent = memoryPool })) { buffer = null; } else { memoryPool.Push(ref buffer); } } else { onReaded(default(fastCSharp.code.cSharp.tcpBase.subByteArrayEvent)); if (memoryPool != null) { memoryPool.Push(ref buffer); } } reader next = FileStream.next(this); if (next == null) { FileStream = null; onReaded = null; memoryPool = null; typePool <reader> .PushNotNull(this); return; } onReaded = next.onReaded; index = next.index; next.onReaded = null; typePool <reader> .PushNotNull(next); }while (true); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <returns>解压缩后的数据</returns> public subArray<byte> GetDeCompress(byte[] compressData, memoryPool memoryPool = null) { if (compressData.length() > 0) { return GetDeCompressUnsafe(compressData, 0, compressData.Length, memoryPool); } return default(subArray<byte>); }