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