package com.uas.report.axis;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import com.uas.report.util.FileUtils;

public class FileResource extends Resource {

	private String fileType;
	private byte[] data;
	private String referenceURI;

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public String getReferenceURI() {
		return referenceURI;
	}

	public void setReferenceURI(String referenceURI) {
		this.referenceURI = referenceURI;
	}

	public void readData(InputStream is) throws IOException {
		setData(FileUtils.readData(is));
	}

	public InputStream getDataStream() {
		return data == null ? null : new ByteArrayInputStream(data);
	}

	public boolean isReference() {
		return referenceURI != null && referenceURI.length() > 0;
	}

	public boolean hasData() {
		// empty array is considered no data
		return !isReference() && data != null && data.length > 0;
	}

	@Override
	protected Class<?> getImplementingItf() {
		return FileResource.class;
	}

	@Override
	public String toString() {
		return "FileResource [fileType=" + fileType + ", data=" + Arrays.toString(data) + "]";
	}

}
