001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/dbaseapi/DBFHeader.java $
002    
003    /*----------------    FILE HEADER  ------------------------------------------
004    
005    This file is part of deegree.
006    Copyright (C) 2001-2008 by:
007    EXSE, Department of Geography, University of Bonn
008    http://www.giub.uni-bonn.de/deegree/
009    lat/lon GmbH
010    http://www.lat-lon.de
011    
012    This library is free software; you can redistribute it and/or
013    modify it under the terms of the GNU Lesser General Public
014    License as published by the Free Software Foundation; either
015    version 2.1 of the License, or (at your option) any later version.
016    
017    This library is distributed in the hope that it will be useful,
018    but WITHOUT ANY WARRANTY; without even the implied warranty of
019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020    Lesser General Public License for more details.
021    
022    You should have received a copy of the GNU Lesser General Public
023    License along with this library; if not, write to the Free Software
024    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
025    
026    Contact:
027    
028    Andreas Poth
029    lat/lon GmbH
030    Aennchenstr. 19
031    53115 Bonn
032    Germany
033    E-Mail: poth@lat-lon.de
034    
035    Prof. Dr. Klaus Greve
036    Department of Geography
037    University of Bonn
038    Meckenheimer Allee 166
039    53115 Bonn
040    Germany
041    E-Mail: greve@giub.uni-bonn.de
042    
043                     
044     ---------------------------------------------------------------------------*/
045    
046    package org.deegree.io.dbaseapi;
047    
048    import org.deegree.model.spatialschema.ByteUtils;
049    
050    
051    
052    /**
053     * Class representing the header of a dBase III/IV file
054     * 
055     * @version 03.05.2000
056     * @author Andreas Poth
057     */
058    public class DBFHeader {
059    
060        private byte[] header = null;    
061    
062       /**
063        * constructor
064        * retrieves number of fields
065        */
066        public DBFHeader(FieldDescriptor[] fieldDesc) throws DBaseException {           
067    
068            try {
069    //          allocate memory for the header
070                header = new byte[32 + fieldDesc.length * 32 + 1];
071    
072                // set file type
073                header[0] = 3;
074    
075                // set date YYMMDD
076                header[1] = 0; 
077                header[2] = 1; 
078                header[3] = 1;
079    
080                //number of records in the dBase file
081                ByteUtils.writeLEInt(header,4,0);
082    
083                // write number of bytes in the header
084                ByteUtils.writeLEShort(header,8,header.length);        
085    
086                // set field descriptors and calculate length of the data section
087                int sum = 0;
088                for (int i = 0; i < fieldDesc.length; i++) {
089                    setField( i, fieldDesc[i] );
090                    byte[] data = fieldDesc[i].getFieldDescriptor();
091                    sum += data[16];
092                    data = null;
093                }
094    
095                sum++;
096    
097                // write number of bytes in the record (data section) 
098                ByteUtils.writeLEShort(header,10,sum);
099    
100                // set field terminator
101                header[header.length-1] = 0x0D;
102            } catch (Exception e) {
103                e.printStackTrace();
104                throw new DBaseException( e.getMessage() );
105            }        
106    
107        }
108    
109       /**
110        * method: public void setField(int index, FieldDescriptor fd)
111        * puts a field on the header byte array
112        */
113        private void setField(int index, FieldDescriptor fd) {
114    
115            // get field descriptor data        
116            byte[] fddata = fd.getFieldDescriptor();
117    
118            // put field descriptor data on header byte array
119            for (int i = 0; i < 32; i++) {
120                header[32 + (index * 32) + i] = fddata[i];    
121            }
122    
123        }
124    
125        /**
126         * method: public byte[] getHeader() throws DBaseException 
127         * returns the header as a byte array
128         * 
129         */
130        public byte[] getHeader()  {
131            return header;
132        }
133    
134    }