01: // IntegerArrayAttribute.java
02: // $Id: IntegerArrayAttribute.java,v 1.4 2000/08/16 21:37:56 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.tools.resources.upgrade;
07:
08: import java.io.DataInputStream;
09: import java.io.DataOutputStream;
10: import java.io.IOException;
11:
12: /**
13: * The generic description of an IntegerArrayAttribute.
14: */
15:
16: public class IntegerArrayAttribute extends Attribute {
17:
18: /**
19: * Turn a IntegerArray attribute into a String.
20: * We use the <em>normal</em> property convention, which is to separate
21: * each item with a <strong>|</strong>.
22: * @return A String based encoding for that value.
23: */
24:
25: public String stringify(Object value) {
26: if ((value == null) || (!(value instanceof int[])))
27: return null;
28: int ai[] = (int[]) value;
29: StringBuffer sb = new StringBuffer();
30: for (int i = 0; i < ai.length; i++) {
31: if (i > 0)
32: sb.append('|');
33: sb.append(ai[i]);
34: }
35: return sb.toString();
36: }
37:
38: /**
39: * Is the given object a valid IntegerArrayAttribute value ?
40: * @param obj The object to test.
41: * @return A boolean <strong>true</strong> if okay.
42: */
43:
44: public boolean checkValue(Object obj) {
45: return (obj instanceof int[]);
46: }
47:
48: /**
49: * Get the number of bytes required to save that attribute value.
50: * @param The value about to be pickled.
51: * @return The number of bytes needed to pickle that value.
52: */
53:
54: public final int getPickleLength(Object value) {
55: return ((int[]) value).length * 4 + 4;
56: }
57:
58: /**
59: * Pickle a integer array to the given output stream.
60: * @param out The output stream to pickle to.
61: * @param obj The object to pickle.
62: * @exception IOException If some IO error occured.
63: */
64:
65: public void pickle(DataOutputStream out, Object ia)
66: throws IOException {
67: int is[] = (int[]) ia;
68: out.writeInt(is.length);
69: for (int i = 0; i < is.length; i++)
70: out.writeInt(is[i]);
71: }
72:
73: /**
74: * Unpickle an integer array from the given input stream.
75: * @param in The input stream to unpickle from.
76: * @return An instance of int[].
77: * @exception IOException If some IO error occured.
78: */
79:
80: public Object unpickle(DataInputStream in) throws IOException {
81: int cnt = in.readInt();
82: int is[] = new int[cnt];
83: for (int i = 0; i < cnt; i++)
84: is[i] = in.readInt();
85: return is;
86: }
87:
88: public IntegerArrayAttribute(String name, String def[],
89: Integer flags) {
90: super (name, def, flags);
91: this .type = "int[]";
92: }
93:
94: }
|