View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
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(//"file:" + 
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 }