2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
7 package org.apidesign.bck2brwsr.dew;
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.io.Reader;
17 import java.io.Writer;
21 * @author Tomas Zeuzla
23 class MemoryFileObject extends BaseFileObject {
25 private byte[] content;
26 private long lastModified;
32 super(resourceName, kind);
33 this.content = content;
34 this.lastModified = this.content == null ?
36 System.currentTimeMillis();
42 this(resourceName, getKind(resourceName) ,content);
47 public InputStream openInputStream() throws IOException {
48 if (content == null) {
49 throw new IOException();
51 return new ByteArrayInputStream(content);
56 public OutputStream openOutputStream() throws IOException {
57 return new CloseStream();
61 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
62 return new InputStreamReader(openInputStream());
66 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
67 if (content == null) {
68 throw new IOException();
70 return new String(content);
75 public Writer openWriter() throws IOException {
76 return new OutputStreamWriter(openOutputStream());
80 public long getLastModified() {
85 public boolean delete() {
93 private class CloseStream extends OutputStream {
95 private final ByteArrayOutputStream delegate;
98 delegate = new ByteArrayOutputStream();
102 public void write(int b) throws IOException {
107 public void write(byte[] b) throws IOException {
112 public void write(byte[] b, int off, int len) throws IOException {
113 delegate.write(b, off, len);
117 public void close() throws IOException {
119 content = delegate.toByteArray();
120 lastModified = System.currentTimeMillis();