01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel.fmt;
21:
22: import java.io.DataInputStream;
23: import java.io.IOException;
24: import java.io.OutputStream;
25:
26: import com.sun.codemodel.JResourceFile;
27:
28: /**
29: * Allows an application to copy a resource file to the output.
30: *
31: * @author
32: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
33: */
34: public final class JStaticFile extends JResourceFile {
35:
36: private final ClassLoader classLoader;
37: private final String resourceName;
38: private final boolean isResource;
39:
40: public JStaticFile(String _resourceName) {
41: this (_resourceName, !_resourceName.endsWith(".java"));
42: }
43:
44: public JStaticFile(String _resourceName, boolean isResource) {
45: this (JStaticFile.class.getClassLoader(), _resourceName,
46: isResource);
47: }
48:
49: /**
50: * @param isResource
51: * false if this is a Java source file. True if this is other resource files.
52: */
53: public JStaticFile(ClassLoader _classLoader, String _resourceName,
54: boolean isResource) {
55: super (_resourceName
56: .substring(_resourceName.lastIndexOf('/') + 1));
57: this .classLoader = _classLoader;
58: this .resourceName = _resourceName;
59: this .isResource = isResource;
60: }
61:
62: protected boolean isResource() {
63: return isResource;
64: }
65:
66: protected void build(OutputStream os) throws IOException {
67: DataInputStream dis = new DataInputStream(classLoader
68: .getResourceAsStream(resourceName));
69:
70: byte[] buf = new byte[256];
71: int sz;
72: while ((sz = dis.read(buf)) > 0)
73: os.write(buf, 0, sz);
74:
75: dis.close();
76: }
77:
78: }
|