Debian Codegraph @

src/main/java/org/freehep/util/io/Action.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5
6 /**
7 * Generic Action, to be used with the TagIn/OutputStreams. An action can have
8 * an ActionCode, a length as well as parameters.
9 *
10 * @author Mark Donszelmann
11 * @author Charles Loomis
12 * @version $Id: Action.java 8584 2006-08-10 23:06:37Z duns $
13 */
14 public abstract class Action {
15
16 private int code;
17
18 private String name;
19
20 protected Action(int code) {
21 this.code = code;
22
23 name = getClass().getName();
24 int dot = name.lastIndexOf(".");
25 name = (dot >= 0) ? name.substring(dot + 1) : name;
26 }
27
28 /**
29 * Read an action from the input, with given actioncode and length
30 *
31 * @param actionCode decoded actionCode
32 * @param input input to read from
33 * @param length length to read
34 * @return action corresponding to actionCode
35 * @throws IOException if read fails
36 */
37 public abstract Action read(int actionCode, TaggedInputStream input,
38 int length) throws IOException;
39
40 /**
41 * Write an action to output
42 *
43 * @param actionCode actionCode to use for this action
44 * @param output output to write to
45 * @throws IOException if write fails
46 */
47 public abstract void write(int actionCode, TaggedOutputStream output)
48 throws IOException;
49
50 /**
51 * @return actionCode
52 */
53 public int getCode() {
54 return code;
55 }
56
57 /**
58 * @return name of the action
59 */
60 public String getName() {
61 return name;
62 }
63
64 public String toString() {
65 return "Action " + getName() + " (" + getCode() + ")";
66 }
67
68 /**
69 * Used for not recognized actions.
70 */
71 public static class Unknown extends Action {
72 private int[] data;
73
74 /**
75 * Create a special Action for Unknown Actions, with actioncode 0.
76 */
77 public Unknown() {
78 super(0x00);
79 }
80
81 /**
82 * Create a special Action for Unknown Actions, with given action code.
83 *
84 * @param actionCode code to be used for Unknown Action.
85 */
86 public Unknown(int actionCode) {
87 super(actionCode);
88 }
89
90 public Action read(int actionCode, TaggedInputStream input, int length)
91 throws IOException {
92
93 Unknown action = new Unknown(actionCode);
94 action.data = input.readUnsignedByte(length);
95 return action;
96 }
97
98 public void write(int actionCode, TaggedOutputStream output)
99 throws IOException {
100
101 output.writeUnsignedByte(data);
102 }
103
104 public String toString() {
105 return super.toString() + " UNKNOWN!, length " + data.length;
106 }
107 }
108
109 }