KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > classfile > ClassFormatOutput


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.classfile.ClassFormatOutput
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.classfile;
23
24 import org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream;
25 import java.io.DataOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29
30 /** A wrapper around DataOutputStream to provide input functions in terms
31     of the types defined on pages 83 of the Java Virtual Machine spec.
32
33     For this types use these methods of DataOutputStream
34     <UL>
35     <LI>float - writeFloat
36     <LI>long - writeLong
37     <LI>double - writeDouble
38     <LI>UTF/String - writeUTF
39     <LI>U1Array - write(byte[])
40     </UL>
41  */

42
43 public final class ClassFormatOutput extends DataOutputStream JavaDoc {
44
45     public ClassFormatOutput() {
46         this(512);
47     }
48
49     public ClassFormatOutput(int size) {
50         this(new AccessibleByteArrayOutputStream(size));
51     }
52     public ClassFormatOutput(java.io.OutputStream JavaDoc stream) {
53         super(stream);
54     }
55     public void putU1(int i) throws IOException JavaDoc {
56         // ensure the format of the class file is not
57
// corrupted by writing an incorrect, truncated value.
58
if (i > 255)
59             ClassFormatOutput.limit("U1", 255, i);
60         write(i);
61     }
62     public void putU2(int i) throws IOException JavaDoc {
63         putU2("U2", i);
64
65     }
66     public void putU2(String JavaDoc limit, int i) throws IOException JavaDoc {
67         
68         // ensure the format of the class file is not
69
// corrupted by writing an incorrect, truncated value.
70
if (i > 65535)
71             ClassFormatOutput.limit(limit, 65535, i);
72         write(i >> 8);
73         write(i);
74     }
75     public void putU4(int i) throws IOException JavaDoc {
76         writeInt(i);
77     }
78
79     public void writeTo(OutputStream outTo) throws IOException JavaDoc {
80         ((AccessibleByteArrayOutputStream) out).writeTo(outTo);
81     }
82
83     /**
84         Get a reference to the data array the class data is being built
85         in. No copy is made.
86     */

87     public byte[] getData() {
88         return ((AccessibleByteArrayOutputStream) out).getInternalByteArray();
89     }
90
91     /**
92      * Throw an ClassFormatError if a limit of the Java class file format is reached.
93      * @param name Terse limit description from JVM spec.
94      * @param limit What the limit is.
95      * @param value What the value for the current class is
96      * @throws IOException Thrown when limit is exceeded.
97      */

98     static void limit(String JavaDoc name, int limit, int value)
99         throws IOException JavaDoc
100     {
101         throw new IOException JavaDoc(name + "(" + value + " > " + limit + ")");
102     }
103 }
104
Popular Tags