001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.index.quadtree.fs;
017:
018: import org.geotools.index.quadtree.StoreException;
019: import java.io.IOException;
020: import java.nio.ByteBuffer;
021: import java.nio.channels.ReadableByteChannel;
022: import java.nio.charset.Charset;
023: import java.util.logging.Logger;
024:
025: /**
026: * DOCUMENT ME!
027: *
028: * @author Tommaso Nolli
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/quadtree/fs/IndexHeader.java $
030: */
031: public class IndexHeader {
032: public static final byte LSB_ORDER = -1;
033: public static final byte MSB_ORDER = -2;
034: public static final byte NATIVE_ORDER = 0;
035: public static final byte NEW_LSB_ORDER = 1;
036: public static final byte NEW_MSB_ORDER = 2;
037: private static final String SIGNATURE = "SQT";
038: private static final byte VERSION = 1;
039: private static final byte[] RESERVED = { 0, 0, 0 };
040: private static final Logger LOGGER = org.geotools.util.logging.Logging
041: .getLogger("org.geotools.index.quadtree");
042: private byte byteOrder;
043:
044: public IndexHeader(byte byteOrder) {
045: this .byteOrder = byteOrder;
046: }
047:
048: /**
049: * DOCUMENT ME!
050: *
051: * @param channel
052: *
053: * @throws IOException
054: * @throws StoreException
055: */
056: public IndexHeader(ReadableByteChannel channel) throws IOException,
057: StoreException {
058: ByteBuffer buf = ByteBuffer.allocate(8);
059:
060: channel.read(buf);
061: buf.flip();
062:
063: byte[] tmp = new byte[3];
064: buf.get(tmp);
065:
066: String s = new String(tmp, "US-ASCII");
067:
068: if (!s.equals(SIGNATURE)) {
069: // Old file format
070: LOGGER.warning("Old qix file format; this file format "
071: + "is deprecated; It is strongly recommended "
072: + "to regenerate it in new format.");
073:
074: buf.position(0);
075: tmp = buf.array();
076:
077: boolean lsb;
078:
079: if ((tmp[4] == 0) && (tmp[5] == 0) && (tmp[6] == 0)
080: && (tmp[7] == 0)) {
081: lsb = !((tmp[0] == 0) && (tmp[1] == 0));
082: } else {
083: lsb = !((tmp[4] == 0) && (tmp[5] == 0));
084: }
085:
086: this .byteOrder = lsb ? LSB_ORDER : MSB_ORDER;
087: } else {
088: this .byteOrder = buf.get();
089: }
090: }
091:
092: public void writeTo(ByteBuffer buf) {
093: Charset charSet = Charset.forName("US-ASCII");
094:
095: ByteBuffer tmp = charSet.encode(SIGNATURE);
096: tmp.position(0);
097: buf.put(tmp);
098: buf.put(this .byteOrder);
099: buf.put(VERSION);
100: buf.put(RESERVED);
101: }
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @return Returns the byteOrder.
107: */
108: public byte getByteOrder() {
109: return this.byteOrder;
110: }
111: }
|