Esempio n. 1
0
        /// <summary>
        /// Retrieve all byte data containing in this block and associated child data blocks.
        /// </summary>
        /// <returns>Byte array.</returns>
        public byte[] GetAllData()
        {
            if (Data == null || Data.Length < 1)
            {
                return(null);
            }
            if (ChildBlock < 0)
            {
                return(Data);
            }

            // get local data
            byte[] ret = new byte[Data.Length];
            Buffer.BlockCopy(Data, 0, ret, 0, Data.Length);
            long currPointer = ChildBlock;

            while (true)
            {
                if (currPointer > 0)
                {
                    byte[]    nextBlockBytes = CfsCommon.ReadFromPosition(Filestream, currPointer, BlockSizeBytes);
                    DataBlock nextBlock      = DataBlock.FromBytes(Filestream, BlockSizeBytes, nextBlockBytes, Logging);
                    if (nextBlock.DataLength < 1 || nextBlock.Data == null || nextBlock.Data.Length < 1)
                    {
                        LogDebug("GetAllData reached end of file in data block at position " + currPointer);
                        break;
                    }
                    else
                    {
                        LogDebug("GetAllData appending " + nextBlock.DataLength + " bytes of data from data block at position " + currPointer);
                        byte[] temp = new byte[(ret.Length + nextBlock.DataLength)];
                        Buffer.BlockCopy(ret, 0, temp, 0, ret.Length);
                        Buffer.BlockCopy(nextBlock.Data, 0, temp, ret.Length, nextBlock.DataLength);
                        ret = temp;

                        // update pointer
                        currPointer = nextBlock.ChildBlock;
                    }
                }
                else
                {
                    LogDebug("GetAllData no child block specified, exiting with " + ret.Length + " bytes of data");
                    break;
                }
            }

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve data from the specified range.
        /// </summary>
        /// <param name="startPosition">The starting position from which to read.</param>
        /// <param name="count">The number of bytes to read.</param>
        /// <returns>Byte data for the requested range.</returns>
        public byte[] GetData(long startPosition, long count)
        {
            if (CfsCommon.IsTrue(IsDirectory))
            {
                throw new InvalidOperationException("Get data must only be called on file metadata blocks");
            }

            if (Data == null || Data.Length < 1)
            {
                return(null);
            }

            byte[] localData = new byte[LocalDataLength];
            Buffer.BlockCopy(Data, 0, localData, 0, LocalDataLength);

            if (ChildDataBlock > 0)
            {
                // get child data
                byte[]    nextBytes = CfsCommon.ReadFromPosition(Filestream, ChildDataBlock, 64);
                DataBlock nextBlock = DataBlock.FromBytes(Filestream, BlockSizeBytes, nextBytes, Logging);
                nextBlock.Filestream = Filestream;
                byte[] childData = nextBlock.GetAllData();

                // join
                if (childData != null)
                {
                    byte[] ret = new byte[(localData.Length + childData.Length)];
                    Buffer.BlockCopy(localData, 0, ret, 0, localData.Length);
                    Buffer.BlockCopy(childData, 0, ret, localData.Length, childData.Length);
                    return(ret);
                }
                else
                {
                    return(localData);
                }
            }
            else
            {
                return(localData);
            }
        }