oraclefilesystem.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 569 行 · 第 1/2 页

JAVA
569
字号
                + "(FSENTRY_PATH, FSENTRY_NAME, FSENTRY_DATA, "                + "FSENTRY_LASTMOD, FSENTRY_LENGTH) "                + "select ?, FSENTRY_NAME, FSENTRY_DATA, "                + "FSENTRY_LASTMOD, FSENTRY_LENGTH from "                + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? "                + "and FSENTRY_LENGTH is not null";    }    /**     * {@inheritDoc}     * <p/>     * Overridden because we need to use <code>oracle.sql.BLOB</code>     * and <code>PreparedStatement#setBlob</code> instead of just     * <code>PreparedStatement#setBinaryStream</code>.     */    public OutputStream getOutputStream(final String filePath) throws FileSystemException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        FileSystemPathUtil.checkFormat(filePath);        final String parentDir = FileSystemPathUtil.getParentDir(filePath);        final String name = FileSystemPathUtil.getName(filePath);        if (!isFolder(parentDir)) {            throw new FileSystemException("path not found: " + parentDir);        }        if (isFolder(filePath)) {            throw new FileSystemException("path denotes folder: " + filePath);        }        try {            TransientFileFactory fileFactory = TransientFileFactory.getInstance();            final File tmpFile = fileFactory.createTransientFile("bin", null, null);            return new FilterOutputStream(new FileOutputStream(tmpFile)) {                public void close() throws IOException {                    super.close();                    InputStream in = null;                    Blob blob = null;                    try {                        if (isFile(filePath)) {                            synchronized (updateDataSQL) {                                long length = tmpFile.length();                                in = new FileInputStream(tmpFile);                                blob = createTemporaryBlob(in);                                executeStmt(updateDataSQL,                                        new Object[]{                                            blob,                                            new Long(System.currentTimeMillis()),                                            new Long(length),                                            parentDir,                                            name                                        });                            }                        } else {                            synchronized (insertFileSQL) {                                long length = tmpFile.length();                                in = new FileInputStream(tmpFile);                                blob = createTemporaryBlob(in);                                executeStmt(insertFileSQL,                                        new Object[]{                                            parentDir,                                            name,                                            blob,                                            new Long(System.currentTimeMillis()),                                            new Long(length)                                        });                            }                        }                    } catch (Exception e) {                        throw new IOException(e.getMessage());                    } finally {                        if (blob != null) {                            try {                                freeTemporaryBlob(blob);                            } catch (Exception e1) {                            }                        }                        if (in != null) {                            try {                                in.close();                            } catch (Exception e1) {                            }                        }                        // temp file can now safely be removed                        tmpFile.delete();                    }                }            };        } catch (Exception e) {            String msg = "failed to open output stream to file: " + filePath;            log.error(msg, e);            throw new FileSystemException(msg, e);        }    }    /**     * {@inheritDoc}     */    public RandomAccessOutputStream getRandomAccessOutputStream(final String filePath) throws FileSystemException, UnsupportedOperationException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        FileSystemPathUtil.checkFormat(filePath);        final String parentDir = FileSystemPathUtil.getParentDir(filePath);        final String name = FileSystemPathUtil.getName(filePath);        if (!isFolder(parentDir)) {            throw new FileSystemException("path not found: " + parentDir);        }        if (isFolder(filePath)) {            throw new FileSystemException("path denotes folder: " + filePath);        }        try {            TransientFileFactory fileFactory = TransientFileFactory.getInstance();            final File tmpFile = fileFactory.createTransientFile("bin", null, null);            // @todo FIXME use java.sql.Blob            if (isFile(filePath)) {                // file entry exists, spool contents to temp file first                InputStream in = getInputStream(filePath);                OutputStream out = new FileOutputStream(tmpFile);                try {                    int read;                    byte[] ba = new byte[8192];                    while ((read = in.read(ba, 0, ba.length)) != -1) {                        out.write(ba, 0, read);                    }                } finally {                    out.close();                    in.close();                }            }            return new RandomAccessOutputStream() {                private final RandomAccessFile raf =                    new RandomAccessFile(tmpFile, "rw");                public void close() throws IOException {                    raf.close();                    InputStream in = null;                    Blob blob = null;                    try {                        if (isFile(filePath)) {                            synchronized (updateDataSQL) {                                long length = tmpFile.length();                                in = new FileInputStream(tmpFile);                                blob = createTemporaryBlob(in);                                executeStmt(updateDataSQL,                                        new Object[]{                                            blob,                                            new Long(System.currentTimeMillis()),                                            new Long(length),                                            parentDir,                                            name                                        });                            }                        } else {                            synchronized (insertFileSQL) {                                long length = tmpFile.length();                                in = new FileInputStream(tmpFile);                                blob = createTemporaryBlob(in);                                executeStmt(insertFileSQL,                                        new Object[]{                                            parentDir,                                            name,                                            blob,                                            new Long(System.currentTimeMillis()),                                            new Long(length)                                        });                            }                        }                    } catch (Exception e) {                        throw new IOException(e.getMessage());                    } finally {                        if (blob != null) {                            try {                                freeTemporaryBlob(blob);                            } catch (Exception e1) {                            }                        }                        if (in != null) {                            try {                                in.close();                            } catch (Exception e1) {                            }                        }                        // temp file can now safely be removed                        tmpFile.delete();                    }                }                public void seek(long position) throws IOException {                    raf.seek(position);                }                public void write(int b) throws IOException {                    raf.write(b);                }                public void flush() /*throws IOException*/ {                    // nop                }                public void write(byte[] b) throws IOException {                    raf.write(b);                }                public void write(byte[] b, int off, int len) throws IOException {                    raf.write(b, off, len);                }            };        } catch (Exception e) {            String msg = "failed to open output stream to file: " + filePath;            log.error(msg, e);            throw new FileSystemException(msg, e);        }    }    //----------------------------------------< oracle-specific blob handling >    /**     * Creates a temporary oracle.sql.BLOB instance via reflection and spools     * the contents of the specified stream.     */    protected Blob createTemporaryBlob(InputStream in) throws Exception {        /*        BLOB blob = BLOB.createTemporary(con, false, BLOB.DURATION_SESSION);        blob.open(BLOB.MODE_READWRITE);        OutputStream out = blob.getBinaryOutputStream();        ...        out.flush();        out.close();        blob.close();        return blob;        */        Method createTemporary = blobClass.getMethod("createTemporary",                new Class[]{Connection.class, Boolean.TYPE, Integer.TYPE});        Object blob = createTemporary.invoke(null,                new Object[]{con, Boolean.FALSE, DURATION_SESSION_CONSTANT});        Method open = blobClass.getMethod("open", new Class[]{Integer.TYPE});        open.invoke(blob, new Object[]{MODE_READWRITE_CONSTANT});        Method getBinaryOutputStream =                blobClass.getMethod("getBinaryOutputStream", new Class[0]);        OutputStream out = (OutputStream) getBinaryOutputStream.invoke(blob, null);        try {            int read;            byte[] buf = new byte[8192];            while ((read = in.read(buf, 0, buf.length)) > -1) {                out.write(buf, 0, read);            }        } finally {            try {                out.flush();            } catch (IOException ioe) {            }            out.close();        }        Method close = blobClass.getMethod("close", new Class[0]);        close.invoke(blob, null);        return (Blob) blob;    }    /**     * Frees a temporary oracle.sql.BLOB instance via reflection.     */    protected void freeTemporaryBlob(Object blob) throws Exception {        // blob.freeTemporary();        Method freeTemporary = blobClass.getMethod("freeTemporary", new Class[0]);        freeTemporary.invoke(blob, null);    }}

⌨️ 快捷键说明

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