⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbasefilereader.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            bufferCheck();

            // read the deleted flag
            char tempDeleted = (char) buffer.get();

            // skip the next bytes
            buffer.position(buffer.position() + header.getRecordLength() - 1); // the
            // 1 is
            // for
            // the
            // deleted
            // flag
            // just
            // read.

            // add the row if it is not deleted.
            if (tempDeleted != '*') {
                foundRecord = true;
            }
        }
    }

    /**
     * Copy the next record into the array starting at offset.
     * 
     * @param entry
     *                Th array to copy into.
     * @param offset
     *                The offset to start at
     * @throws IOException
     *                 If an error occurs.
     * @return The same array passed in.
     */
    public Object[] readEntry(Object[] entry, final int offset)
            throws IOException {
        if (entry.length - offset < header.getNumFields()) {
            throw new ArrayIndexOutOfBoundsException();
        }

        read();

        // retrieve the record length
        final int numFields = header.getNumFields();

        int fieldOffset = 0;
        for (int j = 0; j < numFields; j++) {
            entry[j + offset] = readObject(fieldOffset, j);
            fieldOffset += fieldLengths[j];
        }

        return entry;
    }

    /**
     * Transfer, by bytes, the next record to the writer.
     */
    public void transferTo(DbaseFileWriter writer) throws IOException {
        bufferCheck();
        buffer.limit(buffer.position() + header.getRecordLength());
        writer.channel.write(buffer);
        buffer.limit(buffer.capacity());

        cnt++;
    }

    private void read() throws IOException {
        boolean foundRecord = false;
        while (!foundRecord) {

            bufferCheck();

            // read the deleted flag
            char deleted = (char) buffer.get();
            if (deleted == '*') {
                continue;
            }

            charBuffer.position(0);
            buffer.limit(buffer.position() + header.getRecordLength() - 1);
            decoder.decode(buffer, charBuffer, true);
            buffer.limit(buffer.capacity());
            charBuffer.flip();

            foundRecord = true;
        }

        cnt++;
    }

    /**
     * Copy the next entry into the array.
     * 
     * @param entry
     *                The array to copy into.
     * @throws IOException
     *                 If an error occurs.
     * @return The same array passed in.
     */
    public Object[] readEntry(Object[] entry) throws IOException {
        return readEntry(entry, 0);
    }

    private Object readObject(final int fieldOffset, final int fieldNum)
            throws IOException {
        final char type = fieldTypes[fieldNum];
        final int fieldLen = fieldLengths[fieldNum];
        Object object = null;

        // System.out.println( charBuffer.subSequence(fieldOffset,fieldOffset +
        // fieldLen));

        if (fieldLen > 0) {

            switch (type) {
            // (L)logical (T,t,F,f,Y,y,N,n)
            case 'l':
            case 'L':
                switch (charBuffer.charAt(fieldOffset)) {

                case 't':
                case 'T':
                case 'Y':
                case 'y':
                    object = Boolean.TRUE;
                    break;
                case 'f':
                case 'F':
                case 'N':
                case 'n':
                    object = Boolean.FALSE;
                    break;
                default:

                    throw new IOException("Unknown logical value : '"
                            + charBuffer.charAt(fieldOffset) + "'");
                }
                break;
            // (C)character (String)
            case 'c':
            case 'C':
                // oh, this seems like a lot of work to parse strings...but,
                // For some reason if zero characters ( (int) char == 0 ) are
                // allowed
                // in these strings, they do not compare correctly later on down
                // the
                // line....
                int start = fieldOffset;
                int end = fieldOffset + fieldLen - 1;
                charBuffer.limit(charBuffer.capacity());
                // trim off whitespace and 'zero' chars
                while (start < end) {
                    char c = charBuffer.get(start);
                    if (c == 0 || Character.isWhitespace(c)) {
                        start++;
                    } else
                        break;
                }
                while (end > start) {
                    char c = charBuffer.get(end);
                    if (c == 0 || Character.isWhitespace(c)) {
                        end--;
                    } else
                        break;
                }
                // set up the new indexes for start and end
                charBuffer.position(start).limit(end + 1);
                String s = charBuffer.toString();
                // to support some foreign languages, such as Chinese, we have
                // to convert
                // from ISO-8859-1 to a user provided charset
                if (!stringCharset.displayName().equals("ISO-8859-1"))
                    s = new String(s.getBytes("ISO-8859-1"), stringCharset
                            .displayName());
                // this resets the limit...
                charBuffer.clear();
                object = s;
                break;
            // (D)date (Date)
            case 'd':
            case 'D':
                try {
                    String tempString = charBuffer.subSequence(fieldOffset,
                            fieldOffset + 4).toString();
                    int tempYear = Integer.parseInt(tempString);
                    tempString = charBuffer.subSequence(fieldOffset + 4,
                            fieldOffset + 6).toString();
                    int tempMonth = Integer.parseInt(tempString) - 1;
                    tempString = charBuffer.subSequence(fieldOffset + 6,
                            fieldOffset + 8).toString();
                    int tempDay = Integer.parseInt(tempString);
                    Calendar cal = Calendar.getInstance();
                    cal.clear();
                    cal.set(Calendar.YEAR, tempYear);
                    cal.set(Calendar.MONTH, tempMonth);
                    cal.set(Calendar.DAY_OF_MONTH, tempDay);
                    object = cal.getTime();
                } catch (NumberFormatException nfe) {
                    // todo: use progresslistener, this isn't a grave error.
                }
                break;

            // (F)floating (Double)
            case 'n':
            case 'N':
                try {
                    final Class clazz = header.getFieldClass(fieldNum);
                    final String number = extractNumberString(charBuffer,
                            fieldOffset, fieldLen);
                    if (clazz == Integer.class) {
                        object = new Integer(number);
                        break;
                    } else if (clazz == Long.class) {
                        object = new Long(number);
                        break;
                    }
                    // else will fall through to the floating point number
                } catch (NumberFormatException e) {

                    // todo: use progresslistener, this isn't a grave error.

                    // don't do this!!! the Double parse will be attemted as we
                    // fall
                    // through, so no need to create a new Object. -IanS
                    // object = new Integer(0);

                    // Lets try parsing a long instead...
                    try {
                        object = new Long(extractNumberString(charBuffer,
                                fieldOffset, fieldLen));
                        break;
                    } catch (NumberFormatException e2) {

                    }
                }

            case 'f':
            case 'F': // floating point number
                try {

                    object = new Double(extractNumberString(charBuffer,
                            fieldOffset, fieldLen));
                } catch (NumberFormatException e) {
                    // todo: use progresslistener, this isn't a grave error,
                    // though it
                    // does indicate something is wrong

                    // okay, now whatever we got was truly undigestable. Lets go
                    // with
                    // a zero Double.
                    object = new Double(0.0);
                }
                break;
            default:
                throw new IOException("Invalid field type : " + type);
            }

        }
        return object;
    }

    /**
     * @param charBuffer2
     *                TODO
     * @param fieldOffset
     * @param fieldLen
     */
    private final String extractNumberString(final CharBuffer charBuffer2,
            final int fieldOffset, final int fieldLen) {
        String thing = charBuffer2.subSequence(fieldOffset,
                fieldOffset + fieldLen).toString().trim();
        return thing;
    }

    public static void main(String[] args) throws Exception {
        DbaseFileReader reader = new DbaseFileReader(new ShpFiles(args[0]),
                false, Charset.forName("ISO-8859-1"));
        System.out.println(reader.getHeader());
        int r = 0;
        while (reader.hasNext()) {
            System.out.println(++r + ","
                    + java.util.Arrays.asList(reader.readEntry()));
        }
        reader.close();
    }

    public String id() {
        return getClass().getName();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -