-
Notifications
You must be signed in to change notification settings - Fork 4
/
Hollerith.java
113 lines (97 loc) · 2.84 KB
/
Hollerith.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**! -*- Mode: Java; c-basic-offset: 4 -*-
*
* Copyright (c) 1999 by Rasmus Sten <rasmus@sno.pp.se>
*
*/
package nu.dll.lyskom;
import java.io.Serializable;
/**
* Class representing the LysKOM datatype Hollerith. A Hollerith represents a
* byte vector of arbitrary length containing arbitrary byte values. It is used
* for all handling of strings in LysKOM.
*/
public class Hollerith extends KomToken implements Serializable {
private static final long serialVersionUID = 8782974097151111491L;
String charset = Session.defaultServerEncoding;
/**
* Constructs an empty Hollerith
*/
public Hollerith() {
setContents(null);
}
public Hollerith(String s) {
this(s, Session.defaultServerEncoding);
}
/**
* Creates a new Hollerith using the session server encoding
*/
public Hollerith(String string, Session session) {
this(string, session.getServerEncoding());
}
public Hollerith(byte[] buf, String charset) {
setCharset(charset);
setContents(buf);
}
/**
* Construct a Hollertith by converting the supplied string into bytes
* according to the given charset.
*/
public Hollerith(String s, String charset) {
try {
setCharset(charset);
setContents(s.getBytes(charset));
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding: "
+ e.getMessage());
}
}
protected void setCharset(String c) {
charset = c;
}
protected String getCharset() {
return charset;
}
/**
* Constructs a Hollerith containing the supplied bytes
*/
public Hollerith(byte[] b) {
setContents(b);
}
public String toString() {
return "HOLLERITH(" + getContents().length + "):\""
+ new String(getContents()) + "\"";
}
/**
* Returns this Holleriths contents as a String, translated according to
* it's encoding.
*/
public String getContentString() {
try {
return new String(getContents(), getCharset());
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding: "
+ e.getMessage());
}
}
/**
* Constructs a byte-array representation of this Hollerith suitable for
* sending to the server, over the network (into the sea...). <br>
* A Hollerith is a prefix being the string length and the characted 'H',
* followed by the string data.
*/
public byte[] toNetwork() {
try {
String prefixString = contents.length + "H";
byte[] prefixBytes = prefixString.getBytes("us-ascii");
byte[] contents = getContents();
byte[] output = new byte[contents.length + prefixBytes.length];
System.arraycopy(prefixBytes, 0, output, 0, prefixBytes.length);
System.arraycopy(contents, 0, output, prefixBytes.length,
contents.length);
return output;
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding: "
+ e.getMessage());
}
}
}