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

com.softicar.platform.common.io.resource.static_.StaticResource 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.resource.static_;

import com.softicar.platform.common.io.mime.IMimeType;
import com.softicar.platform.common.io.mime.MimeType;
import com.softicar.platform.common.io.resource.hash.AbstractHashableResource;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Optional;

/**
 * Default implementation of {@link IStaticResource}.
 *
 * @author Alexander Schmidt
 * @author Oliver Richers
 */
public class StaticResource extends AbstractHashableResource implements IStaticResource {

	private final Class anchorClass;
	private final String filename;
	private final Charset charset;

	/**
	 * Constructs a new {@link StaticResource}.
	 *
	 * @param anchorClass
	 *            a {@link Class} that resides in the package in which the
	 *            resource file is located (never null)
	 * @param filename
	 *            the name of the resource file (never null)
	 * @param charset
	 *            the character set of the resource file, if applicable (may be
	 *            null)
	 */
	public StaticResource(Class anchorClass, String filename, Charset charset) {

		this.anchorClass = Objects.requireNonNull(anchorClass);
		this.filename = Objects.requireNonNull(filename);
		this.charset = charset;
	}

	@Override
	public Class getAnchorClass() {

		return anchorClass;
	}

	@Override
	public InputStream getResourceAsStream() {

		return anchorClass.getResourceAsStream(filename);
	}

	@Override
	public IMimeType getMimeType() {

		return MimeType.getByFilenameOrDefault(filename);
	}

	@Override
	public Optional getCharset() {

		return Optional.ofNullable(charset);
	}

	@Override
	public Optional getFilename() {

		return Optional.of(filename);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy