001: package org.jicengine.io;
002:
003: import java.io.*;
004:
005: /**
006: * implements the most general methods of the Resource-interface.
007: *
008: * <p>
009: * Copyright (C) 2004 Timo Laitinen
010: * </p>
011: *
012: * @author Timo Laitinen
013: * @created 2004-09-20
014: * @since JICE-0.10
015: * @version 1.0
016: */
017:
018: public abstract class AbstractResource implements Resource {
019:
020: private String identifier;
021: private String mimeType;
022:
023: /**
024: *
025: */
026: protected AbstractResource(String identifier) {
027: this .identifier = identifier;
028: }
029:
030: public abstract InputStream getInputStream() throws IOException;
031:
032: /**
033: * <p>
034: * Simple implementation of <code>getReader()</code> that
035: * wraps the <code>InputStream</code> obtained from <code>getInputStream()</code>
036: * with a <code>InputStreamReader</code>.
037: * </p>
038: *
039: * <p>
040: * Override this method if a better alternative for creating the <code>Reader</code>
041: * is available.
042: * </p>
043: */
044: public Reader getReader() throws IOException {
045: return new InputStreamReader(getInputStream());
046: }
047:
048: /**
049: * <p>
050: * Determines the availability of the Resource by calling
051: * <code>getInputStream()</code>: resource is available if the method doesn't
052: * throw an exception.
053: * </p>
054: *
055: * <p>
056: * Override this if a more efficient way for determining
057: * the availability is possible.
058: * </p>
059: */
060: public boolean isAvailable() {
061: try {
062: getInputStream();
063: return true;
064: } catch (IOException e) {
065: return false;
066: }
067: }
068:
069: public void writeTo(OutputStream out) throws IOException {
070: InputStream in = getInputStream();
071: int i;
072: while ((i = in.read()) != -1) {
073: out.write(i);
074: }
075: out.close();
076: in.close();
077: }
078:
079: public void writeTo(Writer writer) throws IOException {
080: Reader reader = getReader();
081: BufferedReader buffReader;
082: if (reader instanceof BufferedReader) {
083: buffReader = (BufferedReader) reader;
084: } else {
085: buffReader = new BufferedReader(reader);
086: }
087:
088: BufferedWriter buffWriter;
089: if (writer instanceof BufferedWriter) {
090: buffWriter = (BufferedWriter) writer;
091: } else {
092: buffWriter = new BufferedWriter(writer);
093: }
094:
095: String line;
096: while ((line = buffReader.readLine()) != null) {
097: buffWriter.write(line);
098: buffWriter.write("\n");
099: }
100: buffWriter.close();
101: buffReader.close();
102: }
103:
104: public String getIdentifier() {
105: return this .identifier;
106: }
107:
108: public void setMimeType(String mimeType) {
109: this .mimeType = mimeType;
110: }
111:
112: /**
113: * Returns the mime-type set with setMimeType().
114: */
115: public String getMimeType() {
116: return this .mimeType;
117: }
118:
119: public abstract Resource getResource(String relativePath)
120: throws IOException;
121:
122: /**
123: * @return the identifier
124: */
125: public String toString() {
126: return getIdentifier();
127: }
128: }
|