Search in sources :

Example 11 with StorageException

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);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) StorageException(com.bluenimble.platform.storage.StorageException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with StorageException

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;
}
Also used : Path(java.nio.file.Path) FileUtils(com.bluenimble.platform.FileUtils) Date(java.util.Date) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) ByteBuffer(java.nio.ByteBuffer) StorageException(com.bluenimble.platform.storage.StorageException) ApiContext(com.bluenimble.platform.api.ApiContext) IOUtils(com.bluenimble.platform.IOUtils) JsonArray(com.bluenimble.platform.json.JsonArray) Path(java.nio.file.Path) StorageObject(com.bluenimble.platform.storage.StorageObject) Chunk(com.bluenimble.platform.streams.Chunk) OutputStream(java.io.OutputStream) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ApiOutput(com.bluenimble.platform.api.ApiOutput) DefaultApiStreamSource(com.bluenimble.platform.api.impls.DefaultApiStreamSource) StreamDecorator(com.bluenimble.platform.streams.StreamDecorator) Files(java.nio.file.Files) Channels(java.nio.channels.Channels) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Lang(com.bluenimble.platform.Lang) FileInputStream(java.io.FileInputStream) Folder(com.bluenimble.platform.storage.Folder) JsonObject(com.bluenimble.platform.json.JsonObject) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) MediaTypeUtils(com.bluenimble.platform.api.media.MediaTypeUtils) FileChannel(java.nio.channels.FileChannel) InputStream(java.io.InputStream) StorageException(com.bluenimble.platform.storage.StorageException) StorageException(com.bluenimble.platform.storage.StorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 13 with StorageException

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);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) Chunk(com.bluenimble.platform.streams.Chunk) StorageException(com.bluenimble.platform.storage.StorageException) FileInputStream(java.io.FileInputStream)

Example 14 with StorageException

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);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) StorageException(com.bluenimble.platform.storage.StorageException) FileInputStream(java.io.FileInputStream)

Example 15 with StorageException

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) {
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ValueHolder(com.bluenimble.platform.ValueHolder) StorageException(com.bluenimble.platform.storage.StorageException)

Aggregations

StorageException (com.bluenimble.platform.storage.StorageException)21 StorageObject (com.bluenimble.platform.storage.StorageObject)10 IOException (java.io.IOException)9 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 Storage (com.bluenimble.platform.storage.Storage)8 JsonObject (com.bluenimble.platform.json.JsonObject)7 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)6 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)6 File (java.io.File)6 Folder (com.bluenimble.platform.storage.Folder)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 FileChannel (java.nio.channels.FileChannel)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2 Chunk (com.bluenimble.platform.streams.Chunk)2 ByteBuffer (java.nio.ByteBuffer)2