forked from geodmms/xdgnjobs

?? ?
2010-04-30 6d2edcc80c543290a1695e1eb364ba4ac6c0df1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.ximple.io.dgn7;
 
import com.ximple.util.DgnUtility;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
 
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.ShortBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
 
public class Dgn7fileWriter {
    private static final Logger logger = LogManager.getLogger(Dgn7fileWriter.class);
 
    private Dgn7fileHeader header;
    private FileChannel channel;
    ByteBuffer buffer;
    private ElementType fileElementType = ElementType.UNDEFINED;
    private ByteBuffer headerTransfer;
    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("Dgn7 Writer");
    private int maxElementId = 0;
 
    public Dgn7fileWriter(FileChannel channel, boolean strict, boolean useMemoryMapped, Lock lock)
            throws IOException, Dgn7fileException {
        this.channel = channel;
        this.useMemoryMappedBuffer = useMemoryMapped;
        streamLogger.open();
        randomAccessEnabled = channel instanceof FileChannel;
        this.lock = lock;
        lock.lockRead();
        lock.lockWrite();
        // init(strict);
    }
 
    public Dgn7fileWriter(FileChannel channel, Lock lock) throws IOException, Dgn7fileException {
        this(channel, true, true, lock);
    }
 
    protected boolean hasNext() throws IOException {
        // mark current position
        int position = buffer.position();
 
        // ensure the proper position, regardless of read or handler behavior
        try {
            buffer.position(this.toBufferOffset(record.end));
        } catch (IllegalArgumentException e) {
            logger.warn("position=" + this.toBufferOffset(record.end), e);
 
            return false;
        }
 
        // no more data left
        if (buffer.remaining() < 4) {
            return false;
        }
 
        // looks good
        boolean hasNext = true;
        short type = buffer.getShort();
 
        if (type == -1) {
            hasNext = false;
        }
 
        // reset things to as they were
        buffer.position(position);
 
        return hasNext;
    }
 
    protected Element.FileRecord nextElement() throws IOException, Dgn7fileException {
        // need to update position
        buffer.position(this.toBufferOffset(record.end));
 
        // record header is big endian
        buffer.order(ByteOrder.LITTLE_ENDIAN);
 
        // read shape record header
        int recordNumber = ++maxElementId;
        short signature = buffer.getShort();
 
        // byte type = (byte) (buffer.get() & 0x7f);
        byte type = (byte) ((signature >>> 8) & 0x007f);
 
        // silly Bentley say contentLength is in 2-byte words
        // and ByteByffer uses bytes.
        // track the record location
        int elementLength = (buffer.getShort() * 2) + 4;
 
        if (!buffer.isReadOnly() && !useMemoryMappedBuffer) {
            // capacity is less than required for the record
            // copy the old into the newly allocated
            if (buffer.capacity() < elementLength) {
                this.currentOffset += buffer.position();
 
                ByteBuffer old = buffer;
 
                // ensure enough capacity for one more record header
                buffer = Dgn7fileReader.ensureCapacity(buffer, elementLength, useMemoryMappedBuffer);
                buffer.put(old);
                fill(buffer, channel);
                buffer.position(0);
            } else
 
                // remaining is less than record length
                // compact the remaining data and read again,
                // allowing enough room for one more record header
                if (buffer.remaining() < elementLength) {
                    this.currentOffset += buffer.position();
                    buffer.compact();
                    fill(buffer, channel);
                    buffer.position(0);
                }
        }
 
        // shape record is all little endian
        // buffer.order(ByteOrder.LITTLE_ENDIAN);
        // read the type, handlers don't need it
        ElementType recordType = ElementType.forID(type);
 
        logger.debug("nextElement at " + this.toBufferOffset(record.end) + ":type=" + type);
 
        // this usually happens if the handler logic is bunk,
        // but bad files could exist as well...
 
        /*
         * if (recordType != ElementType.NULL && recordType != fileElementType)
         * {
         *   throw new IllegalStateException("ShapeType changed illegally from " + fileElementType + " to " + recordType);
         * }
         */
 
        // peek at bounds, then reset for handler
        // many handler's may ignore bounds reading, but we don't want to
        // second guess them...
        buffer.mark();
 
        if (recordType.isMultiPoint()) {
            int lowCoorX = buffer.getInt();
 
            lowCoorX = DgnUtility.convertFromDGN(lowCoorX);
            record.minX = DgnUtility.converUnitToCoord(lowCoorX);
 
            int lowCoorY = buffer.getInt();
 
            lowCoorY = DgnUtility.convertFromDGN(lowCoorY);
            record.minY = DgnUtility.converUnitToCoord(lowCoorY);
 
            int lowCoorZ = buffer.getInt();
 
            lowCoorZ = DgnUtility.convertFromDGN(lowCoorZ);
            record.minZ = DgnUtility.converUnitToCoord(lowCoorZ);
 
            int highCoorX = buffer.getInt();
 
            highCoorX = DgnUtility.convertFromDGN(highCoorX);
            record.maxX = DgnUtility.converUnitToCoord(highCoorX);
 
            int highCoorY = buffer.getInt();
 
            highCoorY = DgnUtility.convertFromDGN(highCoorY);
            record.maxY = DgnUtility.converUnitToCoord(highCoorY);
 
            int highCoorZ = buffer.getInt();
 
            highCoorZ = DgnUtility.convertFromDGN(highCoorZ);
            record.maxZ = DgnUtility.converUnitToCoord(highCoorZ);
        }
 
        buffer.reset();
        record.offset = record.end;
 
        // update all the record info.
        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;
        // record.end = this.toFileOffset(buffer.position()) + elementLength;
 
        // mark this position for the reader
        record.start = buffer.position();
 
        // clear any cached record
        record.handler = recordType.getElementHandler();
        record.element = null;
 
        return record;
    }
 
    private void init(boolean strict) throws IOException, Dgn7fileException {
        header = readHeader(channel, strict);
 
        if (useMemoryMappedBuffer) {
            FileChannel fc = channel;
 
            buffer = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
 
            // buffer.position(100);
            buffer.position(header.size());
            this.currentOffset = 0;
        } else {
            // force useMemoryMappedBuffer to false
            this.useMemoryMappedBuffer = false;
 
            // start with 8K buffer
            buffer = ByteBuffer.allocateDirect(8 * 1024);
            fill(buffer, channel);
            buffer.flip();
            this.currentOffset = header.size();
        }
 
        headerTransfer = ByteBuffer.allocate(4);
        headerTransfer.order(ByteOrder.LITTLE_ENDIAN);
 
        // make sure the record end is set now...
        record.end = toFileOffset(buffer.position());
    }
 
    public static Dgn7fileHeader readHeader(FileChannel channel, boolean strict) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
 
        if (fill(buffer, channel) == -1) {
            throw new EOFException("Premature end of header");
        }
 
        buffer.order(ByteOrder.LITTLE_ENDIAN);
 
        int length = buffer.getShort(2) * 2;
        ByteBuffer old = buffer;
 
        old.position(0);
 
        // ensure enough capacity for one more record header
        buffer = ByteBuffer.allocateDirect(length + 4);
        buffer.put(old);
 
        if (fill(buffer, channel) == -1) {
            throw new EOFException("Premature end of header");
        }
 
        buffer.position(0);
 
        Dgn7fileHeader header = new Dgn7fileHeader();
 
        header.read(buffer, strict);
 
        return header;
    }
 
    protected static int fill(ByteBuffer buffer, FileChannel channel) throws IOException {
        int r = buffer.remaining();
 
        // channel reads return -1 when EOF or other error
        // because they a non-blocking reads, 0 is a valid return value!!
        while ((buffer.remaining() > 0) && (r != -1)) {
            r = channel.read(buffer);
        }
 
        if (r == -1) {
            buffer.limit(buffer.position());
        }
 
        return r;
    }
 
    private void allocateBuffers() {
        buffer = ByteBuffer.allocateDirect(16 * 1024);
    }
 
    private void checkShapeBuffer(int size) {
        if (buffer.capacity() < size) {
            if (buffer != null)
                NIOUtilities.clean(buffer);
            buffer = ByteBuffer.allocateDirect(size);
        }
    }
 
    private void drain() throws IOException {
        buffer.flip();
        while (buffer.remaining() > 0)
            channel.write(buffer);
        buffer.flip().limit(buffer.capacity());
    }
 
    private int toBufferOffset(int offset) {
        return (int) (offset - currentOffset);
    }
 
    private int toFileOffset(int offset) {
        return (int) (currentOffset + offset);
    }
 
    public void writeElement(Element element) throws IOException {
        if (element == null) return;
        if (element.getElementType().isComplexElement()) {
            writeTo(element);
            ComplexElement complexElement = (ComplexElement) element;
            for (Element component : complexElement) {
                writeTo(component);
            }
        } else {
            writeTo(element);
        }
    }
 
    private void writeTo(Element element) throws IOException {
        ByteBuffer writeBuffer = ByteBuffer.allocateDirect(element.raw.length * 2);
        writeBuffer.order(ByteOrder.LITTLE_ENDIAN);
        for (short word : element.raw) {
            writeBuffer.putShort(word);
        }
        writeBuffer.rewind();
 
        channel.write(writeBuffer);
    }
 
 
 
    public void toEnd() throws IOException, Dgn7fileException {
        while (hasNext()) {
            nextElement();
        }
    }
 
    public void close() throws IOException {
        lock.unlockWrite();
        lock.unlockRead();
 
        if (channel.isOpen()) {
            channel.close();
            streamLogger.close();
        }
 
        if (buffer instanceof MappedByteBuffer) {
            NIOUtilities.clean(buffer);
        }
 
        channel = null;
        header = null;
    }
 
    public void writeEOF() throws IOException {
        ByteBuffer writeBuffer = ByteBuffer.allocateDirect(2);
        writeBuffer.order(ByteOrder.LITTLE_ENDIAN);
        writeBuffer.putShort((short) -1);
        channel.write(writeBuffer);
 
    }
}