-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOscMessage.java
165 lines (148 loc) · 3.73 KB
/
OscMessage.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.*;
import java.io.*;
/**
*
* OscMessage
* <BR><BR>
* OpenSoundControl message class.
*
* @author Ben Chun [email protected]
* @version 1.0
*/
public class OscMessage {
private String name;
private Vector types;
private Vector arguments;
/**
* Constructor for the OscMessage.
*
* @param name the message name
*/
public OscMessage(String name) {
this.name = name;
types = new Vector();
arguments = new Vector();
}
/**
* Adds a type/argument pair to the list of arguments
*
* @param type the argument data type
* @param argument the argument value
*/
public void addArg(Character type, Object argument) {
types.addElement(type);
arguments.addElement(argument);
}
/**
* Directly sets the type and arg Vectors
*
* @param types a list of types
* @param args a list of arguments matching the types
*/
public void setTypesAndArgs(Vector types, Vector args) {
this.types = types;
this.arguments = args;
}
/**
* Returns an XML representation of the message
*
*/
public String getXml() {
if (types == null)
return "ERROR: Types not set";
String xml = "";
xml += "<MESSAGE NAME=\"" + name + "\">";
Enumeration t = types.elements();
Enumeration a = arguments.elements();
while (t.hasMoreElements()) {
char type = ( (Character)t.nextElement() ).charValue();
if (type == '[')
xml += "<ARRAY>";
else if (type == ']')
xml += "</ARRAY>";
else {
xml += "<ARGUMENT TYPE=\"" + type + "\" ";
switch(type) {
case 'i':
xml += "VALUE=\"" + (Integer)a.nextElement() + "\" />";
break;
case 'f':
xml += "VALUE=\"" + (Float)a.nextElement() + "\" />";
break;
case 'h':
xml += "VALUE=\"" + (Long)a.nextElement() + "\" />";
break;
case 'd':
xml += "VALUE=\"" + (Double)a.nextElement() + "\" />";
break;
case 's':
xml += "VALUE=\"" + (String)a.nextElement() + "\" />";
break;
case 'T':
case 'F':
case 'N':
case 'I':
xml += " />";
break;
}
}
}
xml += "</MESSAGE>";
return xml;
}
/**
* Returns a byte array representation of this message.
*
*/
public byte[] getByteArray() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream stream = new DataOutputStream(baos);
// address (name)
stream.writeBytes( name );
alignStream( baos );
// type tags
stream.writeByte( ',' ); // comma indicates type tags
Enumeration t = types.elements();
while ( t.hasMoreElements() ) {
char type = ( (Character)t.nextElement() ).charValue();
stream.writeByte( type );
}
alignStream( baos );
// values
t = types.elements();
Enumeration a = arguments.elements();
while ( t.hasMoreElements() ) {
char type = ( (Character)t.nextElement() ).charValue();
switch(type) {
case 'i':
stream.writeInt( ((Integer)a.nextElement()).intValue() );
break;
case 'f':
stream.writeFloat( ((Float)a.nextElement()).floatValue() );
break;
case 'h':
stream.writeLong( ((Long)a.nextElement()).longValue() );
break;
case 'd':
stream.writeDouble( ((Double)a.nextElement()).doubleValue() );
break;
case 's':
stream.writeBytes( ((String)a.nextElement()) );
alignStream( baos );
break;
}
}
return baos.toByteArray();
}
/**
* Make the stream end on a 4-byte boundary by padding it with
* null characters.
*
* @param stream The stream to align.
*/
private void alignStream(ByteArrayOutputStream stream) throws IOException {
int pad = 4 - ( stream.size() % 4 );
for (int i = 0; i < pad; i++)
stream.write(0);
}
}