tzezula@1324
|
1 |
/*
|
tzezula@1324
|
2 |
* To change this license header, choose License Headers in Project Properties.
|
tzezula@1324
|
3 |
* To change this template file, choose Tools | Templates
|
tzezula@1324
|
4 |
* and open the template in the editor.
|
tzezula@1324
|
5 |
*/
|
tzezula@1324
|
6 |
|
tzezula@1324
|
7 |
package org.apidesign.bck2brwsr.dew;
|
tzezula@1324
|
8 |
|
tzezula@1324
|
9 |
import java.io.ByteArrayInputStream;
|
tzezula@1324
|
10 |
import java.io.ByteArrayOutputStream;
|
tzezula@1324
|
11 |
import java.io.IOException;
|
tzezula@1324
|
12 |
import java.io.InputStream;
|
tzezula@1324
|
13 |
import java.io.InputStreamReader;
|
tzezula@1324
|
14 |
import java.io.OutputStream;
|
tzezula@1324
|
15 |
import java.io.OutputStreamWriter;
|
tzezula@1324
|
16 |
import java.io.Reader;
|
tzezula@1324
|
17 |
import java.io.Writer;
|
tzezula@1324
|
18 |
|
tzezula@1324
|
19 |
/**
|
tzezula@1324
|
20 |
*
|
tzezula@1324
|
21 |
* @author Tomas Zeuzla
|
tzezula@1324
|
22 |
*/
|
tzezula@1324
|
23 |
class MemoryFileObject extends BaseFileObject {
|
tzezula@1324
|
24 |
|
tzezula@1324
|
25 |
private byte[] content;
|
tzezula@1324
|
26 |
private long lastModified;
|
tzezula@1324
|
27 |
|
tzezula@1324
|
28 |
MemoryFileObject (
|
tzezula@1324
|
29 |
String resourceName,
|
tzezula@1324
|
30 |
Kind kind,
|
tzezula@1324
|
31 |
byte[] content) {
|
tzezula@1324
|
32 |
super(resourceName, kind);
|
tzezula@1324
|
33 |
this.content = content;
|
tzezula@1324
|
34 |
this.lastModified = this.content == null ?
|
tzezula@1324
|
35 |
-1 :
|
tzezula@1324
|
36 |
System.currentTimeMillis();
|
tzezula@1324
|
37 |
}
|
tzezula@1324
|
38 |
|
tzezula@1324
|
39 |
MemoryFileObject (
|
tzezula@1324
|
40 |
String resourceName,
|
tzezula@1324
|
41 |
byte[] content) {
|
tzezula@1324
|
42 |
this(resourceName, getKind(resourceName) ,content);
|
tzezula@1324
|
43 |
}
|
tzezula@1324
|
44 |
|
tzezula@1324
|
45 |
|
tzezula@1324
|
46 |
@Override
|
tzezula@1324
|
47 |
public InputStream openInputStream() throws IOException {
|
tzezula@1324
|
48 |
if (content == null) {
|
tzezula@1324
|
49 |
throw new IOException();
|
tzezula@1324
|
50 |
} else {
|
tzezula@1324
|
51 |
return new ByteArrayInputStream(content);
|
tzezula@1324
|
52 |
}
|
tzezula@1324
|
53 |
}
|
tzezula@1324
|
54 |
|
tzezula@1324
|
55 |
@Override
|
tzezula@1324
|
56 |
public OutputStream openOutputStream() throws IOException {
|
tzezula@1324
|
57 |
return new CloseStream();
|
tzezula@1324
|
58 |
}
|
tzezula@1324
|
59 |
|
tzezula@1324
|
60 |
@Override
|
tzezula@1324
|
61 |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
|
tzezula@1324
|
62 |
return new InputStreamReader(openInputStream());
|
tzezula@1324
|
63 |
}
|
tzezula@1324
|
64 |
|
tzezula@1324
|
65 |
@Override
|
tzezula@1324
|
66 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
tzezula@1324
|
67 |
if (content == null) {
|
tzezula@1324
|
68 |
throw new IOException();
|
tzezula@1324
|
69 |
} else {
|
tzezula@1324
|
70 |
return new String(content);
|
tzezula@1324
|
71 |
}
|
tzezula@1324
|
72 |
}
|
tzezula@1324
|
73 |
|
tzezula@1324
|
74 |
@Override
|
tzezula@1324
|
75 |
public Writer openWriter() throws IOException {
|
tzezula@1324
|
76 |
return new OutputStreamWriter(openOutputStream());
|
tzezula@1324
|
77 |
}
|
tzezula@1324
|
78 |
|
tzezula@1324
|
79 |
@Override
|
tzezula@1324
|
80 |
public long getLastModified() {
|
tzezula@1324
|
81 |
return lastModified;
|
tzezula@1324
|
82 |
}
|
tzezula@1324
|
83 |
|
tzezula@1324
|
84 |
@Override
|
tzezula@1324
|
85 |
public boolean delete() {
|
tzezula@1324
|
86 |
return false;
|
tzezula@1324
|
87 |
}
|
tzezula@1324
|
88 |
|
tzezula@1324
|
89 |
byte[] getContent() {
|
tzezula@1324
|
90 |
return content;
|
tzezula@1324
|
91 |
}
|
tzezula@1324
|
92 |
|
tzezula@1324
|
93 |
private class CloseStream extends OutputStream {
|
tzezula@1324
|
94 |
|
tzezula@1324
|
95 |
private final ByteArrayOutputStream delegate;
|
tzezula@1324
|
96 |
|
tzezula@1324
|
97 |
CloseStream() {
|
tzezula@1324
|
98 |
delegate = new ByteArrayOutputStream();
|
tzezula@1324
|
99 |
}
|
tzezula@1324
|
100 |
|
tzezula@1324
|
101 |
@Override
|
tzezula@1324
|
102 |
public void write(int b) throws IOException {
|
tzezula@1324
|
103 |
delegate.write(b);
|
tzezula@1324
|
104 |
}
|
tzezula@1324
|
105 |
|
tzezula@1324
|
106 |
@Override
|
tzezula@1324
|
107 |
public void write(byte[] b) throws IOException {
|
tzezula@1324
|
108 |
delegate.write(b);
|
tzezula@1324
|
109 |
}
|
tzezula@1324
|
110 |
|
tzezula@1324
|
111 |
@Override
|
tzezula@1324
|
112 |
public void write(byte[] b, int off, int len) throws IOException {
|
tzezula@1324
|
113 |
delegate.write(b, off, len);
|
tzezula@1324
|
114 |
}
|
tzezula@1324
|
115 |
|
tzezula@1324
|
116 |
@Override
|
tzezula@1324
|
117 |
public void close() throws IOException {
|
tzezula@1324
|
118 |
delegate.close();
|
tzezula@1324
|
119 |
content = delegate.toByteArray();
|
tzezula@1324
|
120 |
lastModified = System.currentTimeMillis();
|
tzezula@1324
|
121 |
}
|
tzezula@1324
|
122 |
|
tzezula@1324
|
123 |
}
|
tzezula@1324
|
124 |
|
tzezula@1324
|
125 |
}
|