view src/org/tmatesoft/hg/internal/ByteArrayChannel.java @ 608:e1b29756f901

Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 07 May 2013 21:27:51 +0200
parents 1a7a9a20e1f9
children
line wrap: on
line source
/*
 * Copyright (c) 2011-2013 TMate Software Ltd
 *  
 * 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; version 2 of the License.
 *
 * 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.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact TMate Software at support@hg4j.com
 */
package org.tmatesoft.hg.internal;

import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;

import org.tmatesoft.hg.util.ByteChannel;

/**
 * {@link ByteChannel} implementation that serializes data into a byte array
 * 
 * @author Artem Tikhomirov
 * @author TMate Software Ltd.
 */
public class ByteArrayChannel implements ByteChannel {
	private final List<ByteBuffer> buffers;
	private ByteBuffer target;
	private byte[] result;
	
	public ByteArrayChannel() {
		this(-1);
	}
	
	public ByteArrayChannel(int size) {
		if (size == -1) {
			buffers = new LinkedList<ByteBuffer>();
		} else {
			if (size < 0) {
				throw new IllegalArgumentException(String.valueOf(size));
			}
			buffers = null;
			target = ByteBuffer.allocate(size);
		}
	}

	/*
	 * {@link #toArray()} calls do not clear data collected so far, subsequent {@link #write(ByteBuffer)}  
	 * augment collected content.
	 */
	public int write(ByteBuffer buffer) {
		int rv = buffer.remaining();
		if (buffers == null) {
			target.put(buffer);
		} else {
			ByteBuffer copy = ByteBuffer.allocate(rv);
			copy.put(buffer);
			buffers.add(copy);
		}
		result = null;
		return rv;
	}

	/**
	 * @return content accumulated so far
	 */
	public byte[] toArray() {
		if (result != null) {
			return result;
		}
		if (buffers == null) {
			assert target.hasArray();
			// int total = target.position();
			// System.arraycopy(target.array(), new byte[total]);
			// I don't want to duplicate byte[] for now
			// although correct way of doing things is to make a copy and discard target
			return target.array();
		} else {
			int total = 0;
			for (ByteBuffer bb : buffers) {
				bb.flip();
				total += bb.limit();
			}
			result = new byte[total];
			int off = 0;
			for (ByteBuffer bb : buffers) {
				bb.get(result, off, bb.limit());
				off += bb.limit();
			}
			return result;
		}
	}
}