jtulach@1326
|
1 |
/**
|
jtulach@1326
|
2 |
* Back 2 Browser Bytecode Translator
|
jtulach@1326
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@1326
|
4 |
*
|
jtulach@1326
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jtulach@1326
|
6 |
* it under the terms of the GNU General Public License as published by
|
jtulach@1326
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jtulach@1326
|
8 |
*
|
jtulach@1326
|
9 |
* This program is distributed in the hope that it will be useful,
|
jtulach@1326
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jtulach@1326
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jtulach@1326
|
12 |
* GNU General Public License for more details.
|
jtulach@1326
|
13 |
*
|
jtulach@1326
|
14 |
* You should have received a copy of the GNU General Public License
|
jtulach@1326
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jtulach@1326
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
tzezula@1324
|
17 |
*/
|
tzezula@1324
|
18 |
|
tzezula@1324
|
19 |
package org.apidesign.bck2brwsr.dew;
|
tzezula@1324
|
20 |
|
tzezula@1324
|
21 |
import java.io.ByteArrayInputStream;
|
tzezula@1324
|
22 |
import java.io.ByteArrayOutputStream;
|
tzezula@1324
|
23 |
import java.io.IOException;
|
tzezula@1324
|
24 |
import java.io.InputStream;
|
tzezula@1324
|
25 |
import java.io.InputStreamReader;
|
tzezula@1324
|
26 |
import java.io.OutputStream;
|
tzezula@1324
|
27 |
import java.io.OutputStreamWriter;
|
tzezula@1324
|
28 |
import java.io.Reader;
|
tzezula@1324
|
29 |
import java.io.Writer;
|
tzezula@1324
|
30 |
|
tzezula@1324
|
31 |
/**
|
tzezula@1324
|
32 |
*
|
tzezula@1324
|
33 |
* @author Tomas Zeuzla
|
tzezula@1324
|
34 |
*/
|
tzezula@1324
|
35 |
class MemoryFileObject extends BaseFileObject {
|
tzezula@1324
|
36 |
|
tzezula@1324
|
37 |
private byte[] content;
|
tzezula@1324
|
38 |
private long lastModified;
|
tzezula@1324
|
39 |
|
tzezula@1324
|
40 |
MemoryFileObject (
|
tzezula@1324
|
41 |
String resourceName,
|
tzezula@1324
|
42 |
Kind kind,
|
tzezula@1324
|
43 |
byte[] content) {
|
tzezula@1324
|
44 |
super(resourceName, kind);
|
tzezula@1324
|
45 |
this.content = content;
|
tzezula@1324
|
46 |
this.lastModified = this.content == null ?
|
tzezula@1324
|
47 |
-1 :
|
tzezula@1324
|
48 |
System.currentTimeMillis();
|
tzezula@1324
|
49 |
}
|
tzezula@1324
|
50 |
|
tzezula@1324
|
51 |
MemoryFileObject (
|
tzezula@1324
|
52 |
String resourceName,
|
tzezula@1324
|
53 |
byte[] content) {
|
tzezula@1324
|
54 |
this(resourceName, getKind(resourceName) ,content);
|
tzezula@1324
|
55 |
}
|
tzezula@1324
|
56 |
|
tzezula@1324
|
57 |
|
tzezula@1324
|
58 |
@Override
|
tzezula@1324
|
59 |
public InputStream openInputStream() throws IOException {
|
tzezula@1324
|
60 |
if (content == null) {
|
tzezula@1324
|
61 |
throw new IOException();
|
tzezula@1324
|
62 |
} else {
|
tzezula@1324
|
63 |
return new ByteArrayInputStream(content);
|
tzezula@1324
|
64 |
}
|
tzezula@1324
|
65 |
}
|
tzezula@1324
|
66 |
|
tzezula@1324
|
67 |
@Override
|
tzezula@1324
|
68 |
public OutputStream openOutputStream() throws IOException {
|
tzezula@1324
|
69 |
return new CloseStream();
|
tzezula@1324
|
70 |
}
|
tzezula@1324
|
71 |
|
tzezula@1324
|
72 |
@Override
|
tzezula@1324
|
73 |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
|
tzezula@1324
|
74 |
return new InputStreamReader(openInputStream());
|
tzezula@1324
|
75 |
}
|
tzezula@1324
|
76 |
|
tzezula@1324
|
77 |
@Override
|
tzezula@1324
|
78 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
tzezula@1324
|
79 |
if (content == null) {
|
tzezula@1324
|
80 |
throw new IOException();
|
tzezula@1324
|
81 |
} else {
|
tzezula@1324
|
82 |
return new String(content);
|
tzezula@1324
|
83 |
}
|
tzezula@1324
|
84 |
}
|
tzezula@1324
|
85 |
|
tzezula@1324
|
86 |
@Override
|
tzezula@1324
|
87 |
public Writer openWriter() throws IOException {
|
tzezula@1324
|
88 |
return new OutputStreamWriter(openOutputStream());
|
tzezula@1324
|
89 |
}
|
tzezula@1324
|
90 |
|
tzezula@1324
|
91 |
@Override
|
tzezula@1324
|
92 |
public long getLastModified() {
|
tzezula@1324
|
93 |
return lastModified;
|
tzezula@1324
|
94 |
}
|
tzezula@1324
|
95 |
|
tzezula@1324
|
96 |
@Override
|
tzezula@1324
|
97 |
public boolean delete() {
|
tzezula@1324
|
98 |
return false;
|
tzezula@1324
|
99 |
}
|
tzezula@1324
|
100 |
|
tzezula@1324
|
101 |
byte[] getContent() {
|
tzezula@1324
|
102 |
return content;
|
tzezula@1324
|
103 |
}
|
tzezula@1324
|
104 |
|
tzezula@1324
|
105 |
private class CloseStream extends OutputStream {
|
tzezula@1324
|
106 |
|
tzezula@1324
|
107 |
private final ByteArrayOutputStream delegate;
|
tzezula@1324
|
108 |
|
tzezula@1324
|
109 |
CloseStream() {
|
tzezula@1324
|
110 |
delegate = new ByteArrayOutputStream();
|
tzezula@1324
|
111 |
}
|
tzezula@1324
|
112 |
|
tzezula@1324
|
113 |
@Override
|
tzezula@1324
|
114 |
public void write(int b) throws IOException {
|
tzezula@1324
|
115 |
delegate.write(b);
|
tzezula@1324
|
116 |
}
|
tzezula@1324
|
117 |
|
tzezula@1324
|
118 |
@Override
|
tzezula@1324
|
119 |
public void write(byte[] b) throws IOException {
|
tzezula@1324
|
120 |
delegate.write(b);
|
tzezula@1324
|
121 |
}
|
tzezula@1324
|
122 |
|
tzezula@1324
|
123 |
@Override
|
tzezula@1324
|
124 |
public void write(byte[] b, int off, int len) throws IOException {
|
tzezula@1324
|
125 |
delegate.write(b, off, len);
|
tzezula@1324
|
126 |
}
|
tzezula@1324
|
127 |
|
tzezula@1324
|
128 |
@Override
|
tzezula@1324
|
129 |
public void close() throws IOException {
|
tzezula@1324
|
130 |
delegate.close();
|
tzezula@1324
|
131 |
content = delegate.toByteArray();
|
tzezula@1324
|
132 |
lastModified = System.currentTimeMillis();
|
tzezula@1324
|
133 |
}
|
tzezula@1324
|
134 |
|
tzezula@1324
|
135 |
}
|
tzezula@1324
|
136 |
|
tzezula@1324
|
137 |
}
|