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

📄 textcache.java

📁 hsql是很有名的嵌入式数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * Closes the source file and deletes it if it is not read-only.     */    void purge() throws HsqlException {        uncommittedCache.clear();        try {            if (cacheReadonly) {                close(false);            } else {                if (dataFile != null) {                    dataFile.close();                    dataFile = null;                }                FileUtil.delete(fileName);            }        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR,                              Trace.TextCache_purging_file_error,                              new Object[] {                fileName, e            });        }    }    /**     *     */    public void remove(int pos, PersistentStore store) throws HsqlException {        CachedObject r = (CachedObject) uncommittedCache.remove(pos);        if (r != null) {            return;        }        r = get(pos, store, false);        int length = r.getStorageSize()                     - ScriptWriterText.BYTES_LINE_SEP.length;        rowOut.reset();        HsqlByteArrayOutputStream out = rowOut.getOutputStream();        try {            out.fill(' ', length);            out.write(ScriptWriterText.BYTES_LINE_SEP);            dataFile.seek(pos);            dataFile.write(out.getBuffer(), 0, out.size());        } catch (IOException e) {            throw (Trace.error(Trace.FILE_IO_ERROR, e.toString()));        }        release(r.getPos());    }    // sqlbob -- Allow line breaks in quoted fields.    protected RowInputInterface readObject(int pos) throws IOException {        ByteArray    buffer    = new ByteArray(80);        boolean      blank     = true;        boolean      complete  = false;        int          termCount = 0;        int          firstPos  = pos;        RowInputText textIn    = (RowInputText) rowIn;        try {            int c;            int quoteCount = 0;            pos = findNextUsedLinePos(pos);            if (pos == -1) {                return null;            }            dataFile.seek(pos);            //-- The following should work for DOS, MAC, and Unix line            //-- separators regardless of host OS.            while (true) {                c = dataFile.read();                if (c == -1) {                    // sqlbob -- Allow last line to not have NL.                    if (buffer.length() > 0) {                        complete = !blank;                        if (!cacheReadonly) {                            dataFile.write(                                ScriptWriterText.BYTES_LINE_SEP, 0,                                ScriptWriterText.BYTES_LINE_SEP.length);                        }                    }                    break;                }                if ((termCount == 0) && (c == '\"')                        && (isQuoted || isAllQuoted)) {                    quoteCount++;                }                if ((quoteCount % 2) == 0) {                    //-- Ensure line is complete.                    if ((termCount == 1) || (c == '\n')) {                        //-- Store first line.                        if (ignoreFirst && pos == 0) {                            header = buffer.toString();                            blank  = true;                        }                        if (c == '\n') {                            termCount++;                        }                        //-- Ignore blanks                        if (blank) {                            pos += buffer.length() + termCount;                            buffer.setLength(0);                            textIn.skippedLine();                            continue;                        } else {                            complete = true;                            break;                        }                    }                } else if (termCount == 1) {                    buffer.append('\r');                }                termCount = 0;                if (c == '\r') {                    termCount = 1;                    continue;                }                if (c != ' ') {                    blank = false;                }                buffer.append(c);            }        } catch (Exception e) {            complete = false;        }        if (complete) {            int length = (int) dataFile.getFilePointer() - pos;            ((RowInputText) rowIn).setSource(buffer.toString(), pos, length);            return rowIn;        }        return null;    }    // fredt - new method    /**     * Searches from file pointer, pos, and finds the beginning of the first     * line that contains any non-space character. Increments the row counter     * when a blank line is skipped.     *     * If none found, return -1;     */    int findNextUsedLinePos(int pos) throws IOException {        int     firstPos   = pos;        int     currentPos = pos;        boolean cr         = false;        dataFile.seek(pos);        while (true) {            int next = dataFile.read();            currentPos++;            switch (next) {                case -1 :                    return firstPos == pos ? -1                                           : firstPos;                case '\r' :                    cr = true;                    break;                case '\n' :                    cr = false;                    ((RowInputText) rowIn).skippedLine();                    firstPos = currentPos;                    break;                case ' ' :                    if (cr) {                        cr = false;                        ((RowInputText) rowIn).skippedLine();                    }                    continue;                default :                    return firstPos;            }        }    }    /**     * This is called internally when old rows need to be removed from the     * cache. Text table rows that have not been saved are those that have not     * been committed yet. So we don't save them but add them to the     * uncommitted cache until such time that they are committed or rolled     * back- fredt     */    protected void saveRows(CachedObject[] rows, int offset,                            int count) throws IOException {        if (count == 0) {            return;        }        for (int i = offset; i < offset + count; i++) {            CachedObject r = rows[i];            uncommittedCache.put(r.getPos(), r);            rows[i] = null;        }    }    /**     * In case the row has been moved to the uncommittedCache, removes it.     * Then saves the row as normal.     */    public void saveRow(CachedObject row) throws IOException {        uncommittedCache.remove(row.getPos());        super.saveRow(row);    }    public String getHeader() {        return header;    }    public void setHeader(String header) throws HsqlException {        if (ignoreFirst && fileFreePosition == 0) {            try {                writeHeader(header);                this.header = header;            } catch (IOException e) {                throw new HsqlException(                    e, Trace.getMessage(Trace.GENERAL_IO_ERROR),                    Trace.GENERAL_IO_ERROR);            }            return;        }        throw Trace.error(Trace.TEXT_TABLE_HEADER);    }    private void writeHeader(String header) throws IOException {        byte[] buf       = null;        String firstLine = header + NL;        try {            buf = firstLine.getBytes(stringEncoding);        } catch (UnsupportedEncodingException e) {            buf = firstLine.getBytes();        }        dataFile.write(buf, 0, buf.length);        fileFreePosition = firstLine.length();    }    private class ByteArray {        private byte[] buffer;        private int    buflen;        public ByteArray(int n) {            buffer = new byte[n];            buflen = 0;        }        public void append(int c) {            if (buflen >= buffer.length) {                byte[] newbuf = new byte[buflen + 80];                System.arraycopy(buffer, 0, newbuf, 0, buflen);                buffer = newbuf;            }            buffer[buflen] = (byte) c;            buflen++;        }        public int length() {            return buflen;        }        public void setLength(int l) {            buflen = l;        }        public String toString() {            try {                return new String(buffer, 0, buflen, stringEncoding);            } catch (UnsupportedEncodingException e) {                return new String(buffer, 0, buflen);            }        }    }    public int getLineNumber() {        return ((RowInputText) rowIn).getLineNumber();    }    protected void setFileModified() throws IOException {        fileModified = true;    }}

⌨️ 快捷键说明

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