Пример #1
0
 /// <summary>read the field descriptions</summary>
 /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/>
 private void ReadFieldTypesAndSizes(LittleEndianDataInputStream littleEndianDataInputStream
                                     )
 {
     try
     {
         fields = new List <DBTableField>();
         for (int i = 0; i < numberFields; i++)
         {
             DBTableField pdxTableField = new DBTableField();
             if (pdxTableField.ReadFieldTypeAndSize(littleEndianDataInputStream))
             {
                 fields.Add(pdxTableField);
             }
         }
     }
     catch (Exception e)
     {
         throw new PDXReaderException("Exception in readFields", e);
     }
 }
Пример #2
0
        /// <summary>Read a table field</summary>
        /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/>
        public virtual void Read(DBTableField pdxTableField, InputStream inputStream)
        {
            try
            {
                /*
                 * get the data
                 */
                byte[] data      = new byte[pdxTableField.GetLength()];
                int    bytesRead = inputStream.Read(data);
                if (bytesRead > 0)
                {
                    /*
                     * convert to type
                     */
                    DBTableField.FieldType fieldType = pdxTableField.GetFieldType();
                    switch (fieldType)
                    {
                    case DBTableField.FieldType.A:
                    {
                        value = StringUtil.ReadString(data);
                        break;
                    }

                    case DBTableField.FieldType.D:
                    {
                        // date
                        long d = ByteBuffer.Wrap(data).Order(ByteOrder.LittleEndian).GetShort();
                        value = System.Convert.ToString(d);
                        break;
                    }

                    case DBTableField.FieldType.S:
                    {
                        long s = ByteBuffer.Wrap(data).Order(ByteOrder.LittleEndian).GetShort();
                        value = System.Convert.ToString(s);
                        break;
                    }

                    case DBTableField.FieldType.I:
                    {
                        data[0] = unchecked ((byte)(data[0] & unchecked ((int)(0x7f))));
                        // handle unsigned integers
                        long i = ByteBuffer.Wrap(data).Order(ByteOrder.BigEndian).GetInt();
                        value = System.Convert.ToString(i);
                        break;
                    }

                    case DBTableField.FieldType.C:
                    {
                        // currency
                        double dollars = ByteBuffer.Wrap(data).Order(ByteOrder.BigEndian).GetDouble();
                        value = double.ToString(dollars);
                        break;
                    }

                    case DBTableField.FieldType.M:
                    {
                        // Memo
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.N:
                    {
                        long n = ByteBuffer.Wrap(data).Order(ByteOrder.LittleEndian).GetLong();
                        value = double.ToString(n);
                        break;
                    }

                    case DBTableField.FieldType.L:
                    {
                        // Logical
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.B:
                    {
                        // Binary
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.O:
                    {
                        // OLE
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.E:
                    {
                        // formatted memo
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.G:
                    {
                        // Graphic
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.Bcd:
                    {
                        // BCD
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.Bytes:
                    {
                        // Bytes
                        value = StringUtil.ByteArrayToString(data);
                        break;
                    }

                    case DBTableField.FieldType.Ts:
                    {
                        // milliseconds since Jan 1, 1 AD, convert to UTC time
                        data[0] = unchecked ((byte)(data[0] & unchecked ((int)(0x7f))));
                        // handle unsigned number
                        double dt       = ByteBuffer.Wrap(data).Order(ByteOrder.BigEndian).GetDouble();
                        long   dateTime = (long)dt;
                        if (dateTime == 0)
                        {
                            value = null;
                        }
                        else
                        {
                            dateTime -= 86400000;
                            // millis in 1 day
                            dateTime -= 62135607600000l;
                            // millis from 01.01.1970
                            value = System.Convert.ToString(dateTime);
                        }
                        break;
                    }

                    case DBTableField.FieldType.Auto:
                    {
                        short auto = ByteBuffer.Wrap(data).Order(ByteOrder.LittleEndian).GetShort();
                        value = short.ToString(auto);
                        break;
                    }

                    default:
                    {
                        throw new Exception("Unknown field type '" + fieldType.ToString() + "'");
                    }
                    }
                }
            }
            catch (Exception e)
            {
                throw new PDXReaderException("Exception in read", e);
            }
        }