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.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.InputStreamReader;
037: import java.io.OutputStream;
038: import java.io.OutputStreamWriter;
039: import java.io.Reader;
040: import java.io.Writer;
041: import java.net.URL;
042:
043: import de.intarsys.tools.file.FileTools;
044: import de.intarsys.tools.randomaccess.IRandomAccess;
045: import de.intarsys.tools.randomaccess.RandomAccessByteArray;
046:
047: /**
048: * An adapter from a byte[] object to {@link ILocator}.
049: */
050: public class ByteArrayLocator extends CommonLocator {
051: protected byte[] content;
052:
053: private String localName;
054:
055: private String type;
056:
057: public ByteArrayLocator(byte[] data, String name, String type) {
058: if (data == null) {
059: data = new byte[0];
060: }
061: this .content = data;
062: this .localName = name;
063: this .type = type;
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see de.intarsys.tools.locator.ILocator#getInputStream()
070: */
071: public InputStream getInputStream() {
072: return new ByteArrayInputStream(content);
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see de.intarsys.tools.locator.ILocator#getRandomAccessData()
079: */
080: public IRandomAccess getRandomAccess() {
081: return new RandomAccessByteArray(content) {
082: private boolean changed = false;
083:
084: public void setLength(long newLength) {
085: changed = true;
086: super .setLength(newLength);
087: }
088:
089: public void flush() throws IOException {
090: super .flush();
091: if (changed) {
092: changed = false;
093: content = toByteArray();
094: }
095: }
096: };
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see de.intarsys.tools.locator.ILocator#getChild(java.lang.String)
103: */
104: public ILocator getChild(String name) {
105: return null;
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see de.intarsys.tools.locator.ILocator#isDirectory()
112: */
113: public boolean isDirectory() {
114: return false;
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see de.intarsys.tools.locator.ILocator#getFullName()
121: */
122: public String getFullName() {
123: if (getType() == null) {
124: return getLocalName();
125: } else {
126: return getLocalName() + "." + getType();
127: }
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see de.intarsys.tools.locator.ILocator#getLocalName()
134: */
135: public String getLocalName() {
136: return localName;
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see de.intarsys.tools.locator.ILocator#getTypedName()
143: */
144: public String getTypedName() {
145: return (type == null) ? localName : (localName + "." + type);
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see de.intarsys.tools.locator.ILocator#getOutputStream()
152: */
153: public OutputStream getOutputStream() {
154: return new ByteArrayOutputStream() {
155: public void flush() throws IOException {
156: super .flush();
157: content = buf;
158: }
159:
160: public void close() throws IOException {
161: super .close();
162: content = buf;
163: }
164: };
165: }
166:
167: /*
168: * (non-Javadoc)
169: *
170: * @see de.intarsys.tools.locator.ILocator#getParent()
171: */
172: public ILocator getParent() {
173: return null;
174: }
175:
176: /*
177: * (non-Javadoc)
178: *
179: * @see de.intarsys.tools.locator.ILocator#getReader()
180: */
181: public Reader getReader() {
182: return new InputStreamReader(getInputStream());
183: }
184:
185: /*
186: * (non-Javadoc)
187: *
188: * @see de.intarsys.tools.locator.ILocator#getReader(java.lang.String)
189: */
190: public Reader getReader(String encoding) throws IOException {
191: return new InputStreamReader(getInputStream(), encoding);
192: }
193:
194: /*
195: * (non-Javadoc)
196: *
197: * @see de.intarsys.tools.component.ISynchronizable#isSynchSynchronous()
198: */
199: public boolean isSynchSynchronous() {
200: return false;
201: }
202:
203: /*
204: * (non-Javadoc)
205: *
206: * @see de.intarsys.tools.locator.ILocator#getType()
207: */
208: public String getType() {
209: return type;
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see de.intarsys.tools.locator.ILocator#getWriter()
216: */
217: public Writer getWriter() {
218: return new OutputStreamWriter(getOutputStream());
219: }
220:
221: /*
222: * (non-Javadoc)
223: *
224: * @see de.intarsys.tools.locator.ILocator#getWriter(java.lang.String)
225: */
226: public Writer getWriter(String encoding) throws IOException {
227: return new OutputStreamWriter(getOutputStream(), encoding);
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see de.intarsys.tools.locator.ILocator#exists()
234: */
235: public boolean exists() {
236: return content != null;
237: }
238:
239: /*
240: * (non-Javadoc)
241: *
242: * @see de.intarsys.tools.locator.ILocator#listLocators(de.intarsys.tools.locator.ILocatorNameFilter)
243: */
244: public ILocator[] listLocators(ILocatorNameFilter filter) {
245: return new ILocator[0];
246: }
247:
248: /*
249: * (non-Javadoc)
250: *
251: * @see de.intarsys.tools.component.ISynchronizable#synch()
252: */
253: public void synch() {
254: //
255: }
256:
257: /*
258: * (non-Javadoc)
259: *
260: * @see de.intarsys.tools.locator.ILocator#toURL()
261: */
262: public URL toURL() {
263: return null;
264: }
265:
266: /*
267: * (non-Javadoc)
268: *
269: * @see de.intarsys.tools.component.ISynchronizable#isOutOfSynch()
270: */
271: public boolean isOutOfSynch() {
272: return false;
273: }
274:
275: public void rename(String newName) throws IOException {
276: localName = FileTools.getBaseName(newName);
277: type = FileTools.getExtension(newName, getType());
278: }
279:
280: public void delete() throws IOException {
281: // nothing to do...
282: }
283: }
|