From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/Listing2001.java | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2001.java') 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 -- cgit v1.2.3