com.tinspx.util.io.ByteArrayChannelSource.java Source code

Java tutorial

Introduction

Here is the source code for com.tinspx.util.io.ByteArrayChannelSource.java

Source

/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tinspx.util.io;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.io.ByteProcessor;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.ReadableByteChannel;

/**
 * A {@code ChannelSource} wrapping a {@code byte[]} array. Return the array
 * from {@link #read()}. This class is intended for when the {@code byte[]}
 * array can change. If the array is constant, use
 * {@link ChannelSource#of(byte[])} instead.
 *
 * @author Ian
 */
public abstract class ByteArrayChannelSource extends ChannelSource {

    @Override
    public abstract byte[] read();

    @Override
    public InputStream openStream() {
        return new BAInputStream(read());
    }

    @Override
    public InputStream openBufferedStream() {
        return new BAInputStream(read());
    }

    @Override
    public ReadableByteChannel openChannel() throws IOException {
        return new BAInputStream(read());
    }

    @Override
    public boolean hasKnownSize() {
        return true;
    }

    @Override
    public boolean contentEquals(ByteSource other) throws IOException {
        if (other == this) {
            return true;
        }
        final byte[] bytes = read();
        return contentEquals(other, bytes, 0, bytes.length);
    }

    @Override
    public HashCode hash(HashFunction hashFunction) throws IOException {
        return hashFunction.hashBytes(read());
    }

    @Override
    public <T> T read(ByteProcessor<T> processor) throws IOException {
        final byte[] bytes = read();
        processor.processBytes(bytes, 0, bytes.length);
        return processor.getResult();
    }

    @Override
    public long copyTo(OutputStream output) throws IOException {
        final byte[] bytes = read();
        output.write(bytes, 0, bytes.length);
        return bytes.length;
    }

    @Override
    public long size() throws IOException {
        return read().length;
    }

    @Override
    public boolean isEmpty() throws IOException {
        return read().length == 0;
    }

    @Override
    public String toString() {
        return "ByteArrayChannelSource{" + truncateBase16(read(), 30) + "}";
    }
}