All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.truecommons.io.ChannelOutputStream Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2012 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.truecommons.io;

import edu.umd.cs.findbugs.annotations.CleanupObligation;
import edu.umd.cs.findbugs.annotations.DischargesObligation;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.WritableByteChannel;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.WillCloseWhenClosed;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * Adapts a {@link WritableByteChannel} to the {@code OutputStream} interface.
 *
 * @see    ChannelInputStream
 * @author Christian Schlichtherle
 */
@NotThreadSafe
@CleanupObligation
public class ChannelOutputStream extends OutputStream {

    private final ByteBuffer single = ByteBuffer.allocate(1);

    /**
     * The underlying {@link SeekableByteChannel}.
     * All methods in this class throw a {@link NullPointerException} if this
     * hasn't been initialized.
     */
    protected @Nullable WritableByteChannel channel;

    public ChannelOutputStream(
            final @CheckForNull @WillCloseWhenClosed WritableByteChannel sbc) {
        this.channel = sbc;
    }

    @Override
    public void write(int b) throws IOException {
        single.put(0, (byte) b).clear();
        if (1 != channel.write(single)) throw new IOException("write error");
    }

    @Override
    public final void write(byte[] b) throws IOException {
        write(b, 0, b.length);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (len != channel.write(ByteBuffer.wrap(b, off, len)))
            throw new IOException("write error");
    }

    @Override
    public void flush() throws IOException {
    }

    @Override
    @DischargesObligation
    public void close() throws IOException {
        channel.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy