| 1 | package uk.co.zonetora.javacRunner; | 
| 2 |   | 
| 3 | import java.io.ByteArrayOutputStream; | 
| 4 | import java.io.IOException; | 
| 5 | import java.io.OutputStream; | 
| 6 | import java.net.URI; | 
| 7 |   | 
| 8 | import javax.tools.SimpleJavaFileObject; | 
| 9 |   | 
| 10 | public class JavaClassOutputToByteArray extends SimpleJavaFileObject { | 
| 11 |   | 
| 12 |     private final ByteArrayOutputStream cachedFile; | 
| 13 |      | 
| 14 |     public JavaClassOutputToByteArray(String name) { | 
| 15 |         super(URI.create("string:///" + name.replace('.','/') + Kind.CLASS.extension), Kind.CLASS); | 
| 16 |         this.cachedFile = new ByteArrayOutputStream(512); | 
| 17 |     } | 
| 18 |      | 
| 19 |     @Override | 
| 20 |     public OutputStream openOutputStream() throws IOException { | 
| 21 |         return  cachedFile; | 
| 22 |     } | 
| 23 |      | 
| 24 |     public byte[] getData() { | 
| 25 |         return cachedFile.toByteArray(); | 
| 26 |     } | 
| 27 |      | 
| 28 | } |