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

com.jmatio.io.stream.HeapBufferDataOutputStream Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
/*
 * Code licensed under new-style BSD (see LICENSE).
 * All code up to tags/original: Copyright (c) 2006, Wojciech Gradkowski
 * All code after tags/original: Copyright (c) 2015, DiffPlug
 */
package com.jmatio.io.stream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class HeapBufferDataOutputStream extends ByteArrayOutputStream implements DataOutputStream {
	private static final int BUFFER_SIZE = 1024;

	public ByteBuffer getByteBuffer() throws IOException {
		return ByteBuffer.wrap(super.buf);
	}

	public void write(ByteBuffer byteBuffer) throws IOException {

		byte[] tmp = new byte[BUFFER_SIZE];

		while (byteBuffer.hasRemaining()) {
			int length = Math.min(byteBuffer.remaining(), tmp.length);
			byteBuffer.get(tmp, 0, length);
			write(tmp, 0, length);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy