001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.locator;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035: import java.io.OutputStream;
036: import java.io.OutputStreamWriter;
037: import java.io.Reader;
038: import java.io.Writer;
039: import java.net.URL;
040:
041: import de.intarsys.tools.file.FileTools;
042: import de.intarsys.tools.randomaccess.IRandomAccess;
043:
044: /**
045: * A simple adapter from {@link InputStream} to {@link ILocator}.
046: */
047: public class StreamLocator extends CommonLocator {
048: private final InputStream inputStream;
049:
050: private final OutputStream outputStream;
051:
052: private String type;
053:
054: private String localName;
055:
056: public StreamLocator(InputStream stream, String name, String type) {
057: this (stream, null, name, type);
058: }
059:
060: public StreamLocator(OutputStream stream, String name, String type) {
061: this (null, stream, name, type);
062: }
063:
064: public StreamLocator(InputStream is, OutputStream os, String name,
065: String type) {
066: super ();
067: this .inputStream = is;
068: this .outputStream = os;
069: this .localName = name;
070: this .type = type;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see de.intarsys.tools.locator.ILocator#getChild(java.lang.String)
077: */
078: public ILocator getChild(String child) {
079: return null;
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see de.intarsys.tools.locator.ILocator#isDirectory()
086: */
087: public boolean isDirectory() {
088: return false;
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see de.intarsys.tools.locator.ILocator#getFullName()
095: */
096: public String getFullName() {
097: return getLocalName();
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see de.intarsys.tools.locator.ILocator#getInputStream()
104: */
105: public InputStream getInputStream() throws IOException {
106: return inputStream;
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see de.intarsys.tools.locator.ILocator#getLocalName()
113: */
114: public String getLocalName() {
115: return localName;
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see de.intarsys.tools.locator.ILocator#getTypedName()
122: */
123: public String getTypedName() {
124: return (type == null) ? localName : (localName + "." + type);
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see de.intarsys.tools.component.ISynchronizable#isOutOfSynch()
131: */
132: public boolean isOutOfSynch() {
133: return false;
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see de.intarsys.tools.locator.ILocator#getOutputStream()
140: */
141: public OutputStream getOutputStream() {
142: return outputStream;
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see de.intarsys.tools.locator.ILocator#getParent()
149: */
150: public ILocator getParent() {
151: return null;
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see de.intarsys.tools.locator.ILocator#getReader()
158: */
159: public Reader getReader() throws IOException {
160: return new InputStreamReader(getInputStream());
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see de.intarsys.tools.locator.ILocator#getReader(java.lang.String)
167: */
168: public Reader getReader(String encoding) throws IOException {
169: return new InputStreamReader(getInputStream(), encoding);
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see de.intarsys.tools.component.ISynchronizable#isSynchSynchronous()
176: */
177: public boolean isSynchSynchronous() {
178: return false;
179: }
180:
181: /*
182: * (non-Javadoc)
183: *
184: * @see de.intarsys.tools.locator.ILocator#getType()
185: */
186: public String getType() {
187: return type;
188: }
189:
190: /*
191: * (non-Javadoc)
192: *
193: * @see de.intarsys.tools.locator.ILocator#getWriter()
194: */
195: public Writer getWriter() {
196: return new OutputStreamWriter(getOutputStream());
197: }
198:
199: /*
200: * (non-Javadoc)
201: *
202: * @see de.intarsys.tools.locator.ILocator#getWriter(java.lang.String)
203: */
204: public Writer getWriter(String encoding) throws IOException {
205: return new OutputStreamWriter(getOutputStream(), encoding);
206: }
207:
208: /*
209: * (non-Javadoc)
210: *
211: * @see de.intarsys.tools.locator.ILocator#exists()
212: */
213: public boolean exists() {
214: return inputStream != null;
215: }
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see de.intarsys.tools.locator.ILocator#listLocators(de.intarsys.tools.locator.ILocatorNameFilter)
221: */
222: public ILocator[] listLocators(ILocatorNameFilter filter) {
223: return new ILocator[0];
224: }
225:
226: /*
227: * (non-Javadoc)
228: *
229: * @see de.intarsys.tools.component.ISynchronizable#synch()
230: */
231: public void synch() {
232: //
233: }
234:
235: /*
236: * (non-Javadoc)
237: *
238: * @see de.intarsys.tools.locator.ILocator#toURL()
239: */
240: public URL toURL() {
241: return null;
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see de.intarsys.tools.locator.ILocator#getRandomAccessData()
248: */
249: public IRandomAccess getRandomAccess() throws IOException {
250: throw new UnsupportedOperationException();
251: }
252:
253: public boolean isReadOnly() {
254: if (super .isReadOnly()) {
255: return true;
256: }
257: return outputStream == null;
258: }
259:
260: public void rename(String newName) throws IOException {
261: localName = FileTools.getBaseName(newName);
262: type = FileTools.getExtension(newName);
263: }
264:
265: public void delete() throws IOException {
266: // nothing to do...
267: }
268:
269: }
|