ch.cyberduck.core.cryptomator.ContentWriter.java Source code

Java tutorial

Introduction

Here is the source code for ch.cyberduck.core.cryptomator.ContentWriter.java

Source

package ch.cyberduck.core.cryptomator;

/*
 * Copyright (c) 2002-2016 iterate GmbH. All rights reserved.
 * https://cyberduck.io/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.DisabledConnectionCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.Session;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Write;
import ch.cyberduck.core.io.DefaultStreamCloser;
import ch.cyberduck.core.io.StatusOutputStream;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ContentWriter {

    private final Session<?> session;

    public ContentWriter(final Session<?> session) {
        this.session = session;
    }

    public void write(final Path file, final byte[] content) throws BackgroundException {
        this.write(file, content, new TransferStatus());
    }

    public void write(final Path file, final byte[] content, final TransferStatus status)
            throws BackgroundException {
        final Write<?> write = session._getFeature(Write.class);
        status.setLength(content.length);
        status.setChecksum(write.checksum(file).compute(new ByteArrayInputStream(content), status));
        final StatusOutputStream<?> out = write.write(file, status, new DisabledConnectionCallback());
        try {
            IOUtils.write(content, out);
        } catch (IOException e) {
            throw new DefaultIOExceptionMappingService().map(e);
        } finally {
            new DefaultStreamCloser().close(out);
        }
    }
}