-
Notifications
You must be signed in to change notification settings - Fork 15
/
Type.java
63 lines (54 loc) · 1.74 KB
/
Type.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package simpledb;
import java.text.ParseException;
import java.io.*;
/**
* Class representing a type in SimpleDB.
* Types are static objects defined by this class; hence, the Type
* constructor is private.
*/
public enum Type implements Serializable {
INT_TYPE() {
@Override
public int getLen() {
return 4;
}
@Override
public Field parse(DataInputStream dis) throws ParseException {
try {
return new IntField(dis.readInt());
} catch (IOException e) {
throw new ParseException("couldn't parse", 0);
}
}
}, STRING_TYPE() {
@Override
public int getLen() {
return STRING_LEN+4;
}
@Override
public Field parse(DataInputStream dis) throws ParseException {
try {
int strLen = dis.readInt();
byte bs[] = new byte[strLen];
dis.read(bs);
dis.skipBytes(STRING_LEN-strLen);
return new StringField(new String(bs), STRING_LEN);
} catch (IOException e) {
throw new ParseException("couldn't parse", 0);
}
}
};
public static final int STRING_LEN = 128;
/**
* @return the number of bytes required to store a field of this type.
*/
public abstract int getLen();
/**
* @return a Field object of the same type as this object that has contents
* read from the specified DataInputStream.
* @param dis The input stream to read from
* @throws ParseException if the data read from the input stream is not
* of the appropriate type.
*/
public abstract Field parse(DataInputStream dis) throws ParseException;
}