| | |
| | | * |
| | | * @author Ulysses |
| | | * @version 0.1 |
| | | * @since 2006/5/17 ¤U¤È 01:24:10 |
| | | * @since 2006/5/17 |
| | | */ |
| | | public class Dgn7fileReader { |
| | | private static final Logger logger = LogManager.getLogger(Dgn7fileReader.class); |
| | |
| | | ByteBuffer buffer; |
| | | private ElementType fileElementType = ElementType.UNDEFINED; |
| | | private ByteBuffer headerTransfer; |
| | | private final Record record = new Record(); |
| | | private final Element.FileRecord record = new Element.FileRecord(); |
| | | private final boolean randomAccessEnabled; |
| | | private Lock lock; |
| | | private boolean useMemoryMappedBuffer; |
| | | private long currentOffset = 0L; |
| | | private StreamLogging streamLogger = new StreamLogging("Shapefile Reader"); |
| | | private StreamLogging streamLogger = new StreamLogging("Dgn7 Reader"); |
| | | private int maxElementId = 0; |
| | | |
| | | public Dgn7fileReader(ReadableByteChannel channel, boolean strict, boolean useMemoryMapped, Lock lock) |
| | | public Dgn7fileReader(FileChannel channel, boolean strict, boolean useMemoryMapped, Lock lock) |
| | | throws IOException, Dgn7fileException { |
| | | this.channel = channel; |
| | | this.useMemoryMappedBuffer = useMemoryMapped; |
| | |
| | | init(strict); |
| | | } |
| | | |
| | | public Dgn7fileReader(ReadableByteChannel channel, Lock lock) throws IOException, Dgn7fileException { |
| | | public Dgn7fileReader(FileChannel channel, Lock lock) throws IOException, Dgn7fileException { |
| | | this(channel, true, true, lock); |
| | | } |
| | | |
| | |
| | | return randomAccessEnabled; |
| | | } |
| | | |
| | | public Record nextElement() throws IOException, Dgn7fileException { |
| | | public Element.FileRecord nextElement() throws IOException, Dgn7fileException { |
| | | // need to update position |
| | | buffer.position(this.toBufferOffset(record.end)); |
| | | |
| | |
| | | record.length = elementLength; |
| | | record.signature = signature; |
| | | record.number = recordNumber; |
| | | record.buffer = buffer; |
| | | |
| | | // remember, we read one int already... |
| | | record.end = this.toFileOffset(buffer.position()) + elementLength - 4; |
| | |
| | | } |
| | | } |
| | | |
| | | public Record elementAt(int offset) throws IOException, UnsupportedOperationException, Dgn7fileException { |
| | | public Element.FileRecord elementAt(int offset) throws IOException, UnsupportedOperationException, Dgn7fileException { |
| | | if (randomAccessEnabled) { |
| | | this.goTo(offset); |
| | | |
| | |
| | | while (reader.hasNext()) { |
| | | size++; |
| | | |
| | | Dgn7fileReader.Record record = reader.nextElement(); |
| | | Element.FileRecord record = reader.nextElement(); |
| | | |
| | | if (record.element() != null) { |
| | | Element element = (Element) record.element(); |
| | |
| | | } catch (IOException e) { |
| | | logger.warn("Stop read dgn file", e); |
| | | } catch (Dgn7fileException e) { |
| | | e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| | | logger.warn(e.getMessage(), e); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | |
| | | System.out.println("count=" + count + " size=" + size); |
| | | logger.debug("count=" + count + " size=" + size); |
| | | // reader.close(); |
| | | } catch (IOException ioe) { |
| | | System.out.println(ioe); |
| | | ioe.printStackTrace(); |
| | | logger.warn(ioe.getMessage(), ioe); |
| | | } catch (Dgn7fileException e) { |
| | | e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| | | logger.warn(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | System.exit(0); |
| | | } |
| | | |
| | | public final class Record { |
| | | int length; |
| | | int number = 0; |
| | | int offset; // Relative to the whole file |
| | | int start = 0; // Relative to the current loaded buffer |
| | | short signature = 0; |
| | | |
| | | /** |
| | | * The minimum X value. |
| | | */ |
| | | public double minX; |
| | | |
| | | /** |
| | | * The minimum Y value. |
| | | */ |
| | | public double minY; |
| | | |
| | | /** |
| | | * The minimum Z value. |
| | | */ |
| | | public double minZ; |
| | | |
| | | /** |
| | | * The maximum X value. |
| | | */ |
| | | public double maxX; |
| | | |
| | | /** |
| | | * The maximum Y value. |
| | | */ |
| | | public double maxY; |
| | | |
| | | /** |
| | | * The maximum Z value. |
| | | */ |
| | | public double maxZ; |
| | | |
| | | // ElementType type; |
| | | int end = 0; // Relative to the whole file |
| | | Object element = null; |
| | | IElementHandler handler; |
| | | |
| | | public Object element() { |
| | | if (element == null) { |
| | | buffer.position(start); |
| | | buffer.order(ByteOrder.LITTLE_ENDIAN); |
| | | |
| | | if (handler == null) { |
| | | return null; |
| | | } |
| | | |
| | | element = handler.read(buffer, signature, length); |
| | | } |
| | | |
| | | return element; |
| | | } |
| | | |
| | | public int offset() { |
| | | return offset; |
| | | } |
| | | |
| | | /** |
| | | * A summary of the record. |
| | | */ |
| | | public String toString() { |
| | | return "Record " + number + " length " + length + " bounds " + minX + "," + minY + " " + maxX + "," + maxY; |
| | | } |
| | | } |
| | | } |