KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > bytecode > PaddedInstruction


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.bytecode;
9
10 import org.gjt.jclasslib.io.ByteCodeInput;
11 import org.gjt.jclasslib.io.ByteCodeOutput;
12
13 import java.io.IOException JavaDoc;
14
15 /**
16     Base class for intstructions which need a four byte padding relative
17     to the start of the enclosing code of the parent <tt>Code</tt>
18     attribute before reading immediate arguments.
19  
20     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
21     @version $Revision: 1.5 $ $Date: 2003/08/18 07:58:35 $
22 */

23 public class PaddedInstruction extends AbstractInstruction {
24
25     /**
26         Constructor.
27         @param opcode the opcode.
28      */

29     public PaddedInstruction(int opcode) {
30         super(opcode);
31     }
32
33     /**
34         Get the padded size in bytes of this instruction.
35         @param offset the offset at which this instruction is found.
36         @return the padded size in bytes
37      */

38     public int getPaddedSize(int offset) {
39         return getSize() + paddingBytes(offset + 1);
40     }
41
42     public void read(ByteCodeInput in) throws IOException JavaDoc {
43         super.read(in);
44         
45         int bytesToRead = paddingBytes(in.getBytesRead());
46         for (int i = 0; i < bytesToRead; i++) {
47             in.readByte();
48         }
49     }
50
51     public void write(ByteCodeOutput out) throws IOException JavaDoc {
52         super.write(out);
53         
54         int bytesToWrite = paddingBytes(out.getBytesWritten());
55         for (int i = 0; i < bytesToWrite; i++) {
56             out.writeByte(0);
57         }
58     }
59     
60     private int paddingBytes(int bytesCount) {
61         
62         int bytesToPad = 4 - bytesCount % 4;
63         return (bytesToPad == 4) ? 0 : bytesToPad;
64     }
65 }
66
Popular Tags