/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> internal TmphSubArray<byte> GetCompressUnsafe(byte[] data, int startIndex, int count, int seek = 0, TmphMemoryPool memoryPool = null) { var length = count + seek; if (memoryPool == null) { using (var dataStream = new MemoryStream()) { if (seek != 0) dataStream.Seek(seek, SeekOrigin.Begin); using (var compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { return TmphSubArray<byte>.Unsafe(dataStream.GetBuffer(), seek, (int)dataStream.Position - seek); } } } else { var TmphBuffer = memoryPool.Get(); try { using (var dataStream = TmphMemoryStream.Get(TmphBuffer)) { if (seek != 0) dataStream.Seek(seek, SeekOrigin.Begin); using (var compressStream = getStream(dataStream)) { compressStream.Write(data, startIndex, count); } if (dataStream.Position < length) { var streamBuffer = dataStream.GetBuffer(); if (streamBuffer == TmphBuffer) TmphBuffer = null; return TmphSubArray<byte>.Unsafe(streamBuffer, seek, (int)dataStream.Position - seek); } } } catch (Exception error) { TmphLog.Error.Add(error, null, false); } finally { memoryPool.Push(ref TmphBuffer); } } return default(TmphSubArray<byte>); }
/// <summary> /// 开始读取文件 /// </summary> public unsafe void Read() { do { var readSize = index.Size + sizeof(int); try { if (FileStream.isDisposed == 0) { TmphBuffer = (memoryPool = TmphMemoryPool.GetPool(readSize)).Get(); var fileReader = FileStream.fileReader; var 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(TmphBuffer, 0, readSize) == readSize) { fixed (byte* bufferFixed = TmphBuffer) { if (*(int*)bufferFixed == index.Size) readSize = index.Size; else TmphLog.Default.Add( FileStream.FileName + " index[" + index.Index.toString() + "] size[" + (*(int*)bufferFixed).toString() + "]<>" + index.Size.toString(), false, false); } } else readSize = 0; } } catch (Exception error) { TmphLog.Default.Add(error, null, false); } var onReaded = this.onReaded; if (readSize == index.Size) { if ( onReaded(new TmphTcpBase.TmphSubByteArrayEvent { TmphBuffer = TmphSubArray<byte>.Unsafe(TmphBuffer, sizeof(int), index.Size), Event = memoryPool.PushSubArray })) TmphBuffer = null; else memoryPool.Push(ref TmphBuffer); } else { onReaded(default(TmphTcpBase.TmphSubByteArrayEvent)); if (memoryPool != null) memoryPool.Push(ref TmphBuffer); } var next = FileStream.next(this); if (next == null) { FileStream = null; onReaded = null; memoryPool = null; TmphTypePool<TmphReader>.Push(this); return; } onReaded = next.onReaded; index = next.index; next.onReaded = null; TmphTypePool<TmphReader>.Push(next); } while (true); }
private void setBuffer(int bufferSize) { TmphBuffer = (memoryPool = TmphMemoryPool.GetPool(bufferSize)).Get(); BufferIndex = sizeof(int); }
/// <summary> /// 写入数据 /// </summary> /// <param name="data">数据</param> /// <returns>写入位置,失败返回-1</returns> internal long UnsafeWrite(TmphMemoryPool.TmphPushSubArray data) { flushWait.Reset(); var dataArray = data.Value; TmphInterlocked.NoCheckCompareSetSleep0(ref bufferLock); if (isDisposed == 0) { var fileBufferLength = this.fileBufferLength; this.fileBufferLength += dataArray.Count; if (isWritting == 0) { var length = currentIndex + dataArray.Count; if (length < bufferLength && flushCount == 0) { Buffer.BlockCopy(dataArray.array, dataArray.StartIndex, TmphBuffer, currentIndex, dataArray.Count); checkFlushTime = TmphDate.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; TmphThreadPool.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="fileName">文件全名</param> /// <param name="mode">打开方式</param> /// <param name="fileShare">共享访问方式</param> /// <param name="fileOption">附加选项</param> /// <param name="encoding">文件编码</param> public TmphFileStreamWriter(string fileName, FileMode mode = FileMode.CreateNew, FileShare fileShare = FileShare.None, FileOptions fileOption = FileOptions.None, bool isLog = true, Encoding encoding = null) { if (fileName.Length() == 0) TmphLog.Error.Throw(TmphLog.TmphExceptionType.Null); FileName = fileName; this.isLog = isLog; this.fileShare = fileShare; this.fileOption = fileOption; this.encoding = encoding; memoryPool = TmphMemoryPool.GetPool(bufferLength = (int)TmphFile.BytesPerCluster(fileName)); TmphBuffer = memoryPool.Get(); writeFileHandle = writeFile; open(mode); flushWait = new EventWaitHandle(true, EventResetMode.ManualReset); }
/// <summary> /// 图像缩略切剪 /// </summary> /// <param name="data">图像文件数据</param> /// <param name="width">缩略宽度,0表示与高度同比例</param> /// <param name="height">缩略高度,0表示与宽度同比例</param> /// <param name="type">目标图像文件格式</param> /// <returns>图像缩略文件数据</returns> public static TmphSubArray<byte> Cut(TmphSubArray<byte> data, int width, int height, ImageFormat type, TmphMemoryPool TmphMemoryPool = null, int seek = 0) { if (data.Count != 0 && width > 0 && height > 0 && (width | height) != 0 && seek >= 0) { try { using (MemoryStream memory = new MemoryStream(data.Array, data.StartIndex, data.Count)) { TmphBuilder TmphBuilder = new TmphBuilder(); using (Image TmphImage = TmphBuilder.CreateImage(memory)) return TmphBuilder.Get(ref width, ref height, type, TmphMemoryPool, seek); } } catch (Exception error) { TmphLog.Error.Add(error, null, false); } } return default(TmphSubArray<byte>); }
/// <summary> /// 图像缩略切剪 /// </summary> /// <param name="data">图像文件数据</param> /// <param name="width">缩略宽度,0表示与高度同比例</param> /// <param name="height">缩略高度,0表示与宽度同比例</param> /// <param name="type">目标图像文件格式</param> /// <returns>图像缩略文件数据</returns> public static TmphSubArray<byte> Cut(byte[] data, int width, int height, ImageFormat type, TmphMemoryPool memoryPool = null, int seek = 0) { if (data == null) return default(TmphSubArray<byte>); return Cut(TmphSubArray<byte>.Unsafe(data, 0, data.Length), width, height, type, memoryPool, seek); }
/// <summary> /// 获取缩略图 /// </summary> /// <param name="width">缩略宽度</param> /// <param name="height">缩略高度</param> /// <param name="type">目标图像文件格式</param> /// <returns>缩略图数据流</returns> public TmphSubArray<byte> Get(ref int width, ref int height, ImageFormat type, TmphMemoryPool TmphMemoryPool, int seek) { if (check(ref width, ref height)) { if (TmphMemoryPool == null) { using (MemoryStream stream = new MemoryStream()) { if (seek != 0) stream.Seek(seek, SeekOrigin.Begin); get(stream, width, height, type); return TmphSubArray<byte>.Unsafe(stream.GetBuffer(), seek, (int)stream.Position - seek); } } byte[] buffer = TmphMemoryPool.Get(); try { #if MONO using (MemoryStream stream = memoryStream.Get(buffer)) #else using (MemoryStream stream = TmphMemoryStreamProxy.Get(buffer)) #endif { if (seek != 0) stream.Seek(seek, SeekOrigin.Begin); get(stream, width, height, type); byte[] data = stream.GetBuffer(); if (buffer == data) { buffer = null; //showjim if ((int)stream.Position > data.Length) { TmphLog.Error.Add("Position " + ((int)stream.Position).toString() + " > " + data.Length.toString(), true, false); } return TmphSubArray<byte>.Unsafe(data, seek, (int)stream.Position - seek); } return TmphSubArray<byte>.Unsafe(data, seek, (int)stream.Position - seek); } } finally { TmphMemoryPool.Push(ref buffer); } } return default(TmphSubArray<byte>); }
/// <summary> /// 获取压缩数据 /// </summary> /// <param name="data">数据</param> /// <returns>压缩数据,失败返回null</returns> internal static TmphSubArray<byte> GetCompress(TmphSubArray<byte> data, TmphMemoryPool memoryPool = null, int seek = 0) { if (data.Count > GZipSize) { var compressData = TmphStream.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(TmphSubArray<byte>); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <returns>解压缩后的数据</returns> public TmphSubArray<byte> GetDeCompress(byte[] compressData, int startIndex, int count, TmphMemoryPool memoryPool = null) { if (count > 0) { var range = new TmphArray.TmphRange(compressData.length(), startIndex, count); if (count == range.GetCount) return GetDeCompressUnsafe(compressData, range.SkipCount, count, memoryPool); } TmphLog.Error.Throw(TmphLog.TmphExceptionType.IndexOutOfRange); return default(TmphSubArray<byte>); }
/// <summary> /// 获取解压数据 /// </summary> /// <returns>解压数据</returns> public unsafe TmphSubArray<byte> Get(TmphMemoryPool memoryPool) { if (memoryPool == null) { var data = TmphUnmanagedPool.StreamBuffers.Get(); try { using (dataStream = new TmphUnmanagedStream(data.Byte, TmphUnmanagedPool.StreamBuffers.Size)) { get(); return new TmphSubArray<byte>(dataStream.GetArray()); } } finally { TmphUnmanagedPool.StreamBuffers.Push(ref data); } } else { var data = memoryPool.Get(); try { fixed (byte* dataFixed = data) { using (dataStream = new TmphUnmanagedStream(dataFixed, data.Length)) { get(); if (dataStream.Data == dataFixed) { var TmphBuffer = data; data = null; return TmphSubArray<byte>.Unsafe(TmphBuffer, 0, dataStream.Length); } return new TmphSubArray<byte>(dataStream.GetArray()); } } } finally { memoryPool.Push(ref data); } } }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <returns>解压缩后的数据</returns> public TmphSubArray<byte> GetDeCompress(byte[] compressData, TmphMemoryPool memoryPool = null) { if (compressData.length() > 0) { return GetDeCompressUnsafe(compressData, 0, compressData.Length, memoryPool); } return default(TmphSubArray<byte>); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="stream">数据流</param> /// <returns>解压缩后的数据</returns> public TmphSubArray<byte> GetDeCompress(Stream stream, TmphMemoryPool memoryPool = null) { if (stream != null) { using (var compressStream = getDecompressStream(stream)) { return new TmphDeCompressor { CompressStream = compressStream }.Get(memoryPool); } } return default(TmphSubArray<byte>); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="compressData">压缩数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">解压缩字节数</param> /// <returns>解压缩后的数据</returns> internal TmphSubArray<byte> GetDeCompressUnsafe(byte[] compressData, int startIndex, int count, TmphMemoryPool memoryPool) { using (Stream memoryStream = new MemoryStream(compressData, startIndex, count)) using (var compressStream = getDecompressStream(memoryStream)) { return new TmphDeCompressor { CompressStream = compressStream }.Get(memoryPool); } }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="startIndex">起始位置</param> /// <param name="count">压缩字节数</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> public TmphSubArray<byte> GetCompress(byte[] data, int startIndex, int count, int seek = 0, TmphMemoryPool memoryPool = null) { if (seek >= 0) { if (count == 0) return TmphSubArray<byte>.Unsafe(TmphNullValue<byte>.Array, 0, 0); var range = new TmphArray.TmphRange(data.length(), startIndex, count); if (count == range.GetCount) return GetCompressUnsafe(data, startIndex, count, seek, memoryPool); } TmphLog.Error.Throw(TmphLog.TmphExceptionType.IndexOutOfRange); return default(TmphSubArray<byte>); }
/// <summary> /// 压缩数据 /// </summary> /// <param name="data">原始数据</param> /// <param name="seek">起始位置</param> /// <returns>压缩后的数据,失败返回null</returns> public TmphSubArray<byte> GetCompress(byte[] data, int seek = 0, TmphMemoryPool memoryPool = null) { if (seek < 0) TmphLog.Error.Throw(TmphLog.TmphExceptionType.IndexOutOfRange); if (data != null && data.Length != 0) { return GetCompressUnsafe(data, 0, data.Length, seek, memoryPool); } return default(TmphSubArray<byte>); }