1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dex.reader.util;
18 
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.URI;
23 
24 import javax.tools.SimpleJavaFileObject;
25 
26 /**
27  * {@code MemoryByteCode} represents an in-memory java byte code.
28  */
29 /* package */ class MemoryByteCode extends SimpleJavaFileObject {
30     private ByteArrayOutputStream baos;
31     private final String name;
32 
MemoryByteCode(String name)33     public MemoryByteCode(String name) {
34         super(URI.create("byte:///" + name.replace(".", "/") + ".class"),
35                 Kind.CLASS);
36         this.name = name;
37     }
38 
39     @Override
getName()40     public String getName() {
41         return name;
42     }
43 
getCharContent(boolean ignoreEncodingErrors)44     public CharSequence getCharContent(boolean ignoreEncodingErrors) {
45         throw new IllegalStateException();
46     }
47 
openOutputStream()48     public OutputStream openOutputStream() {
49         baos = new ByteArrayOutputStream();
50         return baos;
51     }
52 
openInputStream()53     public InputStream openInputStream() {
54         throw new IllegalStateException();
55     }
56 
getBytes()57     public byte[] getBytes() {
58         return baos.toByteArray();
59     }
60 }
61