1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ochafik.lang.compiler;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Reader;
29 import java.io.Writer;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32
33 import javax.tools.FileObject;
34
35 public class MemoryFileObject implements FileObject {
36 String path;
37 byte[] content;
38
39 public MemoryFileObject(String path, String content) {
40 this.path = path;
41 this.content = content == null ? null : content.getBytes();
42 }
43 public MemoryFileObject(String path, byte[] content) {
44 this.path = path;
45 this.content = content;
46 }
47 public String getPath() {
48 return path;
49 }
50 public byte[] getContent() {
51 return content;
52 }
53 public boolean delete() {
54 content = null;
55 return true;
56 }
57 public String getCharContent(boolean ignoreEncodingErrors) {
58 return new String(content);
59 }
60 public long getLastModified() {
61 return System.currentTimeMillis();
62 }
63 public String getName() {
64 return getPath();
65 }
66 public InputStream openInputStream() {
67 if (content == null)
68 return null;
69
70 return new ByteArrayInputStream(content);
71 }
72 public OutputStream openOutputStream() {
73 return new ByteArrayOutputStream() {
74 public void close() throws IOException {
75 super.close();
76 content = toByteArray();
77 }
78 };
79 }
80 public Reader openReader(boolean ignoreEncodingErrors) {
81 InputStream in = openInputStream();
82 return in == null ? null : new InputStreamReader(in);
83 }
84 public Writer openWriter() {
85 OutputStream out = openOutputStream();
86 return out == null ? null : new OutputStreamWriter(out);
87 }
88 public URI toUri() {
89 try {
90 return new URI(
91 getPath());
92 } catch (URISyntaxException ex) {
93 ex.printStackTrace();
94 return null;
95 }
96 }
97 @Override
98 public String toString() {
99 return getPath() + ":\n" + getCharContent(true);
100 }
101 }