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

com.softicar.platform.common.io.buffer.ByteBuffer Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.io.buffer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Supplier;

/**
 * A wrapper around the primitive type byte[].
 *
 * @author Alexander Schmidt
 * @author Oliver Richers
 */
public class ByteBuffer {

	private final byte[] bytes;

	/**
	 * Constructs a new {@link ByteBuffer} from the supplied
	 * {@link InputStream}.
	 *
	 * @param inputStreamSupplier
	 *            a {@link Supplier} of the {@link InputStream} from which the
	 *            bytes shall be read (never null)
	 */
	public ByteBuffer(Supplier inputStreamSupplier) {

		this(readAllBytes(inputStreamSupplier));
	}

	/**
	 * Constructs a new {@link ByteBuffer} from the given bytes.
	 *
	 * @param bytes
	 *            the bytes to buffer (never null)
	 */
	public ByteBuffer(byte[] bytes) {

		this.bytes = bytes;
	}

	/**
	 * Returns the bytes.
	 *
	 * @return the bytes as byte[] (never null)
	 */
	public byte[] getBytes() {

		return bytes;
	}

	/**
	 * Returns a {@link ByteArrayInputStream} for the content.
	 * 

* The caller is obliged to close the returned {@link InputStream}. * * @return a new {@link ByteArrayInputStream}, created from the buffered * bytes (never null) */ public InputStream getInputStream() { return new ByteArrayInputStream(this.bytes); } private static byte[] readAllBytes(Supplier inputStreamSupplier) { try (var inputStream = inputStreamSupplier.get()) { return inputStream.readAllBytes(); } catch (IOException exception) { throw new RuntimeException(exception); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy