예제 #1
0
        public static byte[] WriteToNDB(WDBSheet sheet)
        {
            NDBHeader header = new NDBHeader();

            header.fieldCount = sheet.FieldCount;
            header.lineCount  = sheet.LineCount;

            int structSize = MarshalUtility.GetStructSize(typeof(NDBHeader));

            header.fieldOffset = structSize;

            byte[] fieldBytes = GetFieldBytes(sheet, out var lineSize);
            header.lineSize = lineSize;

            header.lineOffset = structSize + fieldBytes.Length;

            byte[] strBytes  = GetStringBytes(sheet, out var strOffsetDic);
            byte[] lineBytes = GetLineBytes(sheet, strOffsetDic);

            header.stringOffset = structSize + fieldBytes.Length + lineBytes.Length;

            MemoryStream stream = new MemoryStream();

            byte[] headerBytes = MarshalUtility.StructToByte(header, structSize);
            stream.Write(headerBytes, 0, headerBytes.Length);
            stream.Write(fieldBytes, 0, fieldBytes.Length);
            stream.Write(lineBytes, 0, lineBytes.Length);
            stream.Write(strBytes, 0, strBytes.Length);

            return(stream.ToArray());;
        }
예제 #2
0
        private void SetDataInternal(byte[] dataBytes)
        {
            this.dataBytes = dataBytes;

            int headerSize = MarshalUtility.GetStructSize(typeof(NDBHeader));

            if (dataBytes == null || dataBytes.Length < headerSize)
            {
                throw new ArgumentNullException($"The databytes is empty. Name = {Name}");
            }
            header = MarshalUtility.ByteToStruct <NDBHeader>(dataBytes, 0, headerSize);

            fields = new NDBField[header.fieldCount];
            int fieldOffset = 0;
            int byteOffset  = 0;

            for (int i = 0; i < header.fieldCount; ++i)
            {
                NDBField field = NDBByteUtility.ReadField(dataBytes, header.fieldOffset + byteOffset, out int offset);

                fields[i]    = field;
                field.Offset = fieldOffset;
                fieldOffset += NDBFieldUtil.GetFieldOffset(field.FieldType);
                fieldNameToIndexDic.Add(field.Name, i);

                byteOffset += offset;
            }

            for (int i = 0; i < header.lineCount; ++i)
            {
                int id = ByteReader.ReadInt(dataBytes, header.lineOffset + header.lineSize * i, out int offset);
                dataIDToIndexDic.Add(id, i);
            }
        }