com.softicar.platform.common.io.resource.in.memory.InMemoryResource Maven / Gradle / Ivy
Show all versions of platform-common Show documentation
package com.softicar.platform.common.io.resource.in.memory;
import com.softicar.platform.common.io.mime.IMimeType;
import com.softicar.platform.common.io.resource.IResource;
import com.softicar.platform.common.io.resource.hash.AbstractHashableResource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Optional;
/**
* An {@link IResource} that holds its content in a byte array.
*
* @author Oliver Richers
*/
public class InMemoryResource extends AbstractHashableResource {
private final byte[] content;
private final IMimeType mimeType;
private String filename;
private Charset charset;
/**
* Constructs this {@link InMemoryResource}.
*
* Per default, no filename and character set will be defined.
*
* @param content
* the content of the {@link IResource} (never null)
* @param mimeType
* the {@link IMimeType} of the {@link IResource} (never
* null)
*/
public InMemoryResource(byte[] content, IMimeType mimeType) {
this.content = Objects.requireNonNull(content);
this.mimeType = Objects.requireNonNull(mimeType);
this.filename = null;
this.charset = null;
}
@Override
public InputStream getResourceAsStream() {
return new ByteArrayInputStream(content);
}
@Override
public IMimeType getMimeType() {
return mimeType;
}
@Override
public Optional getCharset() {
return Optional.ofNullable(charset);
}
@Override
public Optional getFilename() {
return Optional.ofNullable(filename);
}
/**
* Defines the optional filename of this {@link IResource}.
*
* @param filename
* the filename of this {@link IResource} (may be null)
* @return this
*/
public InMemoryResource setFilename(String filename) {
this.filename = filename;
return this;
}
/**
* Defines the optional character set of this {@link IResource}.
*
* @param charset
* the character set of this {@link IResource} (may be
* null)
* @return this
*/
public InMemoryResource setCharset(Charset charset) {
this.charset = charset;
return this;
}
}