01: // FileAttribute.java
02: // $Id: FileAttribute.java,v 1.4 2000/08/16 21:37:55 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.File;
11: import java.io.IOException;
12:
13: /**
14: * The generic description of an FileAttribute.
15: */
16:
17: public class FileAttribute extends Attribute {
18:
19: /**
20: * Is the given object a valid FileAttribute value ?
21: * @param obj The object to test.
22: * @return A boolean <strong>true</strong> if okay.
23: */
24:
25: public boolean checkValue(Object obj) {
26: return (obj instanceof File);
27: }
28:
29: /**
30: * Get the number of bytes required to save that attribute value.
31: * @param The value about to be pickled.
32: * @return The number of bytes needed to pickle that value.
33: */
34:
35: public final int getPickleLength(Object value) {
36: String str = ((File) value).getPath();
37: int strlen = str.length();
38: int utflen = 0;
39:
40: for (int i = 0; i < strlen; i++) {
41: int c = str.charAt(i);
42: if ((c >= 0x0001) && (c <= 0x007F)) {
43: utflen++;
44: } else if (c > 0x07FF) {
45: utflen += 3;
46: } else {
47: utflen += 2;
48: }
49: }
50: return utflen + 2;
51: }
52:
53: /**
54: * Pickle an File to the given output stream.
55: * @param out The output stream to pickle to.
56: * @param obj The object to pickle.
57: * @exception IOException If some IO error occured.
58: */
59:
60: public void pickle(DataOutputStream out, Object f)
61: throws IOException {
62: if (f instanceof String)
63: out.writeUTF(f.toString());
64: else
65: out.writeUTF(((File) f).getPath());
66: }
67:
68: /**
69: * Unpickle an File from the given input stream.
70: * @param in The input stream to unpickle from.
71: * @return An instance of File.
72: * @exception IOException If some IO error occured.
73: */
74:
75: public Object unpickle(DataInputStream in) throws IOException {
76: return new File(in.readUTF());
77: }
78:
79: public FileAttribute(String name, File def, Integer flags) {
80: super (name, def, flags);
81: this .type = "java.io.File";
82: }
83:
84: }
|