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

📄 textcache.java

📁 纯Java的数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (row != null) {            return;        }        row = cache.release(pos);        clearRowImage(row);        release(pos);    }    private void clearRowImage(CachedObject row) throws IOException {        int length = row.getStorageSize()                     - ScriptWriterText.BYTES_LINE_SEP.length;        rowOut.reset();        HsqlByteArrayOutputStream out = rowOut.getOutputStream();        out.fill(' ', length);        out.write(ScriptWriterText.BYTES_LINE_SEP);        dataFile.seek(row.getPos());        dataFile.write(out.getBuffer(), 0, out.size());    }    public synchronized void removePersistence(int pos,            PersistentStore store) throws IOException {        CachedObject row = (CachedObject) uncommittedCache.get(pos);        if (row != null) {            return;        }        row = cache.get(pos);        clearRowImage(row);    }    protected synchronized RowInputInterface readObject(int pos)    throws IOException {        ByteArray buffer   = new ByteArray(80);        boolean   complete = false;        boolean   wasCR    = false;        int       c;        boolean   hasQuote  = false;        boolean   wasNormal = false;        pos = findNextUsedLinePos(pos);        if (pos == -1) {            return null;        }        dataFile.seek(pos);        while (!complete) {            wasNormal = false;            c         = dataFile.read();            if (c == -1) {                if (buffer.length() == 0) {                    return null;                }                complete = true;                if (wasCR) {                    break;                }                if (!cacheReadonly) {                    dataFile.write(ScriptWriterText.BYTES_LINE_SEP, 0,                                   ScriptWriterText.BYTES_LINE_SEP.length);                }                break;            }            switch (c) {                case DOUBLE_QUOTE_CHAR :                    wasNormal = true;                    complete  = wasCR;                    wasCR     = false;                    if (isQuoted) {                        hasQuote = !hasQuote;                    }                    break;                case CR_CHAR :                    wasCR = !hasQuote;                    break;                case LF_CHAR :                    complete = !hasQuote;                    break;                default :                    wasNormal = true;                    complete  = wasCR;                    wasCR     = false;            }            buffer.append(c);        }        if (complete) {            int length = (int) dataFile.getFilePointer() - pos;            if (wasNormal) {                length--;            }            ((RowInputText) rowIn).setSource(buffer.toString(), pos, length);            return rowIn;        }        return null;    }    public int readHeaderLine() throws HsqlException {        boolean   complete  = false;        boolean   wasCR     = false;        boolean   wasNormal = false;        ByteArray buffer    = new ByteArray(80);        while (!complete) {            wasNormal = false;            int c;            try {                c = dataFile.read();                if (c == -1) {                    if (buffer.length() == 0) {                        return 0;                    }                    complete = true;                    if (!cacheReadonly) {                        dataFile.write(ScriptWriterText.BYTES_LINE_SEP, 0,                                       ScriptWriterText.BYTES_LINE_SEP.length);                    }                    break;                }            } catch (IOException e) {                throw Trace.error(Trace.TEXT_FILE);            }            switch (c) {                case CR_CHAR :                    wasCR = true;                    break;                case LF_CHAR :                    complete = true;                    break;                default :                    wasNormal = true;                    complete  = wasCR;                    wasCR     = false;            }            buffer.append(c);        }        header = buffer.toString();        try {            int length = (int) dataFile.getFilePointer();            if (wasNormal) {                length--;            }            return length;        } catch (IOException e) {            throw Trace.error(Trace.TEXT_FILE);        }    }    // 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 wasCR      = false;        dataFile.seek(pos);        while (true) {            int c = dataFile.read();            currentPos++;            switch (c) {                case CR_CHAR :                    wasCR = true;                    break;                case LF_CHAR :                    wasCR = false;                    ((RowInputText) rowIn).skippedLine();                    firstPos = currentPos;                    break;                case ' ' :                    if (wasCR) {                        wasCR = false;                        ((RowInputText) rowIn).skippedLine();                    }                    break;                case -1 :                    return -1;                default :                    return firstPos;            }        }    }    public synchronized void add(CachedObject object) throws IOException {        super.add(object);        clearRowImage(object);    }    public synchronized CachedObject get(int i, PersistentStore store,                                         boolean keep) throws HsqlException {        if (i < 0) {            return null;        }        CachedObject o = (CachedObject) uncommittedCache.get(i);        if (o == null) {            o = super.get(i, store, keep);        }/*        if (o == null) {            o = super.get(i, store, keep);        }*/        return o;    }    /**     * 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 synchronized 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 synchronized 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 = buf.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 + -