summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java
diff options
context:
space:
mode:
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java')
-rw-r--r--Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java
new file mode 100644
index 0000000..14ae58a
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java
@@ -0,0 +1,76 @@
+/* Listing2001.java */
+
+import java.io.*;
+
+class ClassFileReader
+{
+ private RandomAccessFile f;
+
+ public ClassFileReader(String name)
+ throws IOException
+ {
+ if (!name.endsWith(".class")) {
+ name += ".class";
+ }
+ f = new RandomAccessFile(name,"r");
+ }
+
+ public void close()
+ {
+ if (f != null) {
+ try {
+ f.close();
+ } catch (IOException e) {
+ //nichts
+ }
+ }
+ }
+
+ public void printSignature()
+ throws IOException
+ {
+ String ret = "";
+ int b;
+
+ f.seek(0);
+ for (int i=0; i<4; ++i) {
+ b = f.read();
+ ret += (char)(b/16+'A'-10);
+ ret += (char)(b%16+'A'-10);
+ }
+ System.out.println(
+ "Signatur...... "+
+ ret
+ );
+ }
+
+ public void printVersion()
+ throws IOException
+ {
+ int minor, major;
+
+ f.seek(4);
+ minor = f.readShort();
+ major = f.readShort();
+ System.out.println(
+ "Version....... "+
+ major+"."+minor
+ );
+ }
+}
+
+public class Listing2001
+{
+ public static void main(String[] args)
+ {
+ ClassFileReader f;
+
+ try {
+ f = new ClassFileReader("Listing2001");
+ f.printSignature();
+ f.printVersion();
+ } catch (IOException e) {
+ System.out.println(e.toString());
+ }
+ }
+} \ No newline at end of file