Beispiel #1
0
 /// <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);
 }
Beispiel #2
0
        /// <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));
        }
Beispiel #3
0
            /// <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);
            }