databasefilesystem.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,559 行 · 第 1/4 页
JAVA
1,559 行
ResultSet rs = null; try { Statement stmt = executeStmt( selectFolderExistSQL, new Object[]{parentDir, name}); rs = stmt.getResultSet(); // a folder exists if the result set has at least one entry return rs.next(); } catch (SQLException e) { String msg = "failed to check existence of folder: " + path; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public long lastModified(String path) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(path); String parentDir = FileSystemPathUtil.getParentDir(path); String name = FileSystemPathUtil.getName(path); synchronized (selectLastModifiedSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectLastModifiedSQL, new Object[]{parentDir, name}); rs = stmt.getResultSet(); if (!rs.next()) { throw new FileSystemException("no such file system entry: " + path); } return rs.getLong(1); } catch (SQLException e) { String msg = "failed to determine lastModified of file system entry: " + path; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public long length(String filePath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(filePath); String parentDir = FileSystemPathUtil.getParentDir(filePath); String name = FileSystemPathUtil.getName(filePath); synchronized (selectLengthSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectLengthSQL, new Object[]{parentDir, name}); rs = stmt.getResultSet(); if (!rs.next()) { throw new FileSystemException("no such file: " + filePath); } return rs.getLong(1); } catch (SQLException e) { String msg = "failed to determine length of file: " + filePath; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public boolean hasChildren(String path) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(path); if (!exists(path)) { throw new FileSystemException("no such file system entry: " + path); } synchronized (selectChildCountSQL) { ResultSet rs = null; try { Statement stmt = executeStmt(selectChildCountSQL, new Object[]{path}); rs = stmt.getResultSet(); if (!rs.next()) { return false; } int count = rs.getInt(1); if (FileSystemPathUtil.denotesRoot(path)) { // ingore file system root entry count--; } return (count > 0); } catch (SQLException e) { String msg = "failed to determine child count of file system entry: " + path; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public String[] list(String folderPath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(folderPath); if (!isFolder(folderPath)) { throw new FileSystemException("no such folder: " + folderPath); } synchronized (selectFileAndFolderNamesSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectFileAndFolderNamesSQL, new Object[]{folderPath}); rs = stmt.getResultSet(); ArrayList names = new ArrayList(); while (rs.next()) { String name = rs.getString(1); if (name.length() == 0 && FileSystemPathUtil.denotesRoot(folderPath)) { // this is the file system root entry, skip... continue; } names.add(name); } return (String[]) names.toArray(new String[names.size()]); } catch (SQLException e) { String msg = "failed to list child entries of folder: " + folderPath; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public String[] listFiles(String folderPath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(folderPath); if (!isFolder(folderPath)) { throw new FileSystemException("no such folder: " + folderPath); } synchronized (selectFileNamesSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectFileNamesSQL, new Object[]{folderPath}); rs = stmt.getResultSet(); ArrayList names = new ArrayList(); while (rs.next()) { names.add(rs.getString(1)); } return (String[]) names.toArray(new String[names.size()]); } catch (SQLException e) { String msg = "failed to list file entries of folder: " + folderPath; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public String[] listFolders(String folderPath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(folderPath); if (!isFolder(folderPath)) { throw new FileSystemException("no such folder: " + folderPath); } synchronized (selectFolderNamesSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectFolderNamesSQL, new Object[]{folderPath}); rs = stmt.getResultSet(); ArrayList names = new ArrayList(); while (rs.next()) { String name = rs.getString(1); if (name.length() == 0 && FileSystemPathUtil.denotesRoot(folderPath)) { // this is the file system root entry, skip... continue; } names.add(name); } return (String[]) names.toArray(new String[names.size()]); } catch (SQLException e) { String msg = "failed to list folder entries of folder: " + folderPath; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } } /** * {@inheritDoc} */ public void touch(String filePath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(filePath); String parentDir = FileSystemPathUtil.getParentDir(filePath); String name = FileSystemPathUtil.getName(filePath); int count = 0; synchronized (updateLastModifiedSQL) { try { Statement stmt = executeStmt(updateLastModifiedSQL, new Object[]{ new Long(System.currentTimeMillis()), parentDir, name}); count = stmt.getUpdateCount(); } catch (SQLException e) { String msg = "failed to touch file: " + filePath; log.error(msg, e); throw new FileSystemException(msg, e); } } if (count == 0) { throw new FileSystemException("no such file: " + filePath); } } /** * {@inheritDoc} */ public InputStream getInputStream(String filePath) throws FileSystemException { if (!initialized) { throw new IllegalStateException("not initialized"); } FileSystemPathUtil.checkFormat(filePath); String parentDir = FileSystemPathUtil.getParentDir(filePath); String name = FileSystemPathUtil.getName(filePath); synchronized (selectDataSQL) { try { Statement stmt = executeStmt( selectDataSQL, new Object[]{parentDir, name}); final ResultSet rs = stmt.getResultSet(); if (!rs.next()) { throw new FileSystemException("no such file: " + filePath); } InputStream in = rs.getBinaryStream(1); /** * return an InputStream wrapper in order to * close the ResultSet when the stream is closed */ return new FilterInputStream(in) { public void close() throws IOException { super.close(); // close ResultSet closeResultSet(rs); } }; } catch (SQLException e) { String msg = "failed to retrieve data of file: " + filePath; log.error(msg, e); throw new FileSystemException(msg, e); } } } /** * {@inheritDoc} */ 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; try { if (isFile(filePath)) { synchronized (updateDataSQL) { long length = tmpFile.length(); in = new FileInputStream(tmpFile); executeStmt(updateDataSQL, new Object[]{ new SizedInputStream(in, length), new Long(System.currentTimeMillis()), new Long(length), parentDir, name }); } } else { synchronized (insertFileSQL) { long length = tmpFile.length(); in = new FileInputStream(tmpFile); executeStmt(insertFileSQL, new Object[]{ parentDir, name, new SizedInputStream(in, length), new Long(System.currentTimeMillis()), new Long(length) }); } } } catch (Exception e) { throw new IOException(e.getMessage()); } finally { if (in != null) { in.close(); } // 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);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?