@Override public long getLastModified() { final FileSystem fs = getHadoopFileSystem(); try { return fs.getFileStatus(getHadoopPath()).getModificationTime(); } catch (Exception e) { throw wrapException(e); } finally { FileHelper.safeClose(fs); } }
@Override public boolean isExists() { final FileSystem fs = getHadoopFileSystem(); try { return fs.exists(getHadoopPath()); } catch (Exception e) { throw wrapException(e); } finally { FileHelper.safeClose(fs); } }
@Override public OutputStream write() throws ResourceException { final FileSystem fs = getHadoopFileSystem(); try { final FSDataOutputStream out = fs.create(getHadoopPath(), true); return new HdfsFileOutputStream(out, fs); } catch (IOException e) { // we can close 'fs' in case of an exception FileHelper.safeClose(fs); throw wrapException(e); } }
@Override public long getSize() { final FileSystem fs = getHadoopFileSystem(); try { if (fs.getFileStatus(getHadoopPath()).isFile()) { return fs.getFileStatus(getHadoopPath()).getLen(); } else { return fs.getContentSummary(getHadoopPath()).getLength(); } } catch (Exception e) { throw wrapException(e); } finally { FileHelper.safeClose(fs); } }
@Override public InputStream read() throws ResourceException { final FileSystem fs = getHadoopFileSystem(); final InputStream in; try { final Path hadoopPath = getHadoopPath(); // return a wrapper InputStream which manages the 'fs' closeable if (fs.getFileStatus(hadoopPath).isFile()) { in = fs.open(hadoopPath); return new HdfsFileInputStream(in, fs); } else { return new HdfsDirectoryInputStream(hadoopPath, fs); } } catch (Exception e) { // we can close 'fs' in case of an exception FileHelper.safeClose(fs); throw wrapException(e); } }
@Override public OutputStream append() throws ResourceException { final FileSystem fs = getHadoopFileSystem(); try { final FSDataOutputStream out = fs.append(getHadoopPath()); return new HdfsFileOutputStream(out, fs); } catch (IOException e) { // we can close 'fs' in case of an exception FileHelper.safeClose(fs); throw wrapException(e); } }