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

org.bndtools.templating.BytesResource Maven / Gradle / Ivy

The newest version!
package org.bndtools.templating;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class BytesResource implements Resource {

	private final byte[]	data;
	private final String	encoding;

	public BytesResource(byte[] data, String encoding) {
		this.data = data;
		this.encoding = encoding;
	}

	public BytesResource(byte[] data) {
		this.data = data;
		this.encoding = Charset.defaultCharset()
			.name();
	}

	public static BytesResource loadFrom(InputStream input) throws IOException {
		return loadFrom(input, Charset.defaultCharset()
			.name());
	}

	public static BytesResource loadFrom(InputStream input, String encoding) throws IOException {
		byte[] buf = new byte[1024];
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int count = input.read(buf);
		while (count >= 0) {
			out.write(buf, 0, count);
			count = input.read(buf);
		}
		return new BytesResource(out.toByteArray(), encoding);
	}

	@Override
	public ResourceType getType() {
		return ResourceType.File;
	}

	@Override
	public InputStream getContent() throws IOException {
		return new ByteArrayInputStream(data);
	}

	@Override
	public String getTextEncoding() {
		return encoding;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy