use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.
the class FileSystemStorageObject method pipe.
@Override
public void pipe(InputStream is, long position, long length) throws StorageException {
if (!source.isFile()) {
throw new StorageException("can't pipe a folder object");
}
if (length == 0) {
return;
}
if (position < 0) {
position = 0;
}
FileOutputStream out = null;
try {
out = new FileOutputStream(source);
FileChannel outChannel = out.getChannel();
if (length < 0) {
ReadableByteChannel inChannel = Channels.newChannel(is);
long skipped = IOUtils.skip(inChannel, position);
if (skipped < position) {
return;
}
ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
while (inChannel.read(buffer) != -1) {
buffer.flip();
outChannel.write(buffer);
buffer.compact();
}
buffer.flip();
while (buffer.hasRemaining()) {
outChannel.write(buffer);
}
} else {
ReadableByteChannel inChannel = Channels.newChannel(is);
outChannel.transferFrom(inChannel, position, length);
}
} catch (IOException ex) {
throw new StorageException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(out);
}
}
use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.
the class FileSystemStorageObject method length.
@Override
public long length() throws StorageException {
if (source.isFile()) {
return source.length();
}
long length = 0;
Stream<Path> walkStream = null;
try {
walkStream = Files.walk(source.toPath());
length = walkStream.filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum();
} catch (Exception e) {
throw new StorageException(e.getMessage(), e);
} finally {
if (walkStream != null)
walkStream.close();
}
return length;
}
use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.
the class FileSystemStorageObject method pipe.
@Override
public void pipe(OutputStream os, StreamDecorator decorator, Chunk... chunks) throws StorageException {
if (!source.isFile()) {
throw new StorageException("can't pipe a folder object");
}
FileInputStream in = null;
try {
in = new FileInputStream(source);
FileChannel channel = in.getChannel();
if (decorator != null) {
decorator.start();
}
for (int i = 0; i < chunks.length; i++) {
Chunk chunk = chunks[i];
if (decorator != null) {
decorator.pre(chunk, i);
}
channel.transferTo(chunk.start(), chunk.end() == Chunk.Infinite ? (chunk.end() - chunk.start() + 1) : channel.size(), Channels.newChannel(os));
if (decorator != null) {
decorator.post(chunk, i);
}
}
if (decorator != null) {
decorator.end();
}
} catch (IOException ex) {
throw new StorageException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(in);
}
}
use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.
the class FileSystemStorageObject method pipe.
@Override
public void pipe(final OutputStream os, long position, long length) throws StorageException {
if (!source.isFile()) {
throw new StorageException("can't pipe a folder object");
}
if (length == 0) {
return;
}
if (position < 0) {
position = 0;
}
FileInputStream in = null;
try {
in = new FileInputStream(source);
FileChannel channel = in.getChannel();
channel.transferTo(position, length > 0 ? length : channel.size(), Channels.newChannel(os));
} catch (IOException ex) {
throw new StorageException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(in);
}
}
use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.
the class FileSystemFolder method list.
@Override
public void list(Visitor visitor, Filter filter) throws StorageException {
if (visitor == null) {
return;
}
final ValueHolder<FileSystemStorageObject> vh = new ValueHolder<FileSystemStorageObject>();
vh.set(new FileSystemStorageObject());
DirectoryStream<Path> stream = null;
try {
stream = Files.newDirectoryStream(source.toPath(), new DirectoryStream.Filter<Path>() {
public boolean accept(Path path) throws IOException {
vh.get().setSource(path.toFile());
return filter == null ? true : filter.accept(vh.get());
}
});
if (stream == null) {
return;
}
for (Path path : stream) {
vh.get().setSource(path.toFile());
if (vh.get().isFolder()) {
try {
vh.set(vh.get().toFolder());
} catch (StorageException e) {
throw new IOException(e.getMessage(), e);
}
}
visitor.visit(vh.get());
}
} catch (IOException e) {
throw new StorageException(e.getMessage(), e);
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException ex) {
}
}
}
Aggregations