/* MediaserverBlock.java * * Copyright (c) 2003, Benja Fallenstein and Tuomas J. Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein and Tuomas J. Lukka */ package gzz.mediaserver; import gzz.util.*; import java.io.*; import java.util.*; public interface MediaserverBlock { /** Get the ID of this block. */ Mediaserver.Id getId(); /** Get the MIME Content-Type. */ String getContentType() throws IOException; /** Get the header as a Map, in the format returned * by HeaderUtil.readHeader(). */ Map getHeader() throws IOException; /** Get the raw header as a string. */ String getRawHeader() throws IOException; /** Get an input stream for reading the body of this block. */ InputStream getInputStream() throws IOException; /** Get an input stream for reading the whole raw data * in this block. */ InputStream getRawInputStream() throws IOException; /** Get the name of the pool that this block came from. */ String getPoolName(); byte[] getBytes() throws IOException; byte[] getRaw() throws IOException; static abstract class AbstractBlock implements MediaserverBlock { /** * @param is An unchecked input stream on the raw data, * for reading in the header. An id-checked input stream * would throw an exception when closed because we read * not the whole block, but just the header. */ protected AbstractBlock(Mediaserver.Id id, String poolName, InputStream is) throws IOException { this.id = id; this.poolName = poolName; header = HeaderUtil.readHeader(is); is.close(); contentType = HeaderUtil.getOne(header, "Content-Type"); } private Mediaserver.Id id; private String poolName; private String contentType; private Map header; public Mediaserver.Id getId() { return id; } public String getContentType() { return contentType; } public Map getHeader() { return header; } public String getRawHeader() throws IOException { InputStream is = getRawInputStream(); String s = HeaderUtil.readRaw(is); while(is.read() >= 0); is.close(); return s; } public InputStream getInputStream() throws IOException { InputStream is = getRawInputStream(); HeaderUtil.readHeader(is); return is; } public String getPoolName() { return poolName; } public byte[] getBytes() throws IOException { return CopyUtil.readBytes(getInputStream()); } public byte[] getRaw() throws IOException { return CopyUtil.readBytes(getRawInputStream()); } } static class ByteArrayBlock extends AbstractBlock { byte[] rawBytes; ByteArrayBlock(Mediaserver.Id id, String poolName, byte[] rawBytes) throws IOException { super(id, poolName, new ByteArrayInputStream(rawBytes)); if(!IDSpace.instance.checkData(id, rawBytes)) throw new Mediaserver.InvalidID("Invalid raw data for block!"); this.rawBytes = rawBytes; } public InputStream getRawInputStream() { return new ByteArrayInputStream(rawBytes); } } static class FileBlock extends AbstractBlock { File file; FileBlock(Mediaserver.Id id, String poolName, File file) throws IOException { super(id, poolName, new FileInputStream(file)); this.file = file; } public InputStream getRawInputStream() throws IOException { return IDSpace.instance.checkInputStream(this.getId(), new FileInputStream(file)); } } }