Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f84e4412cfe2bb13b93d7f12912d4414561e542 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.eclipse.net4j.test.protocol;


import org.eclipse.net4j.core.Channel;
import org.eclipse.net4j.util.ImplementationError;

import java.util.ArrayList;
import java.util.List;


public class ValueHelper
{
  public static final byte END_OF_TRANSMISSION = -1;

  public static final byte BOOLEAN = 1;

  public static final byte CHARACTER = 2;

  public static final byte BYTE = 3;

  public static final byte SHORT = 4;

  public static final byte INTEGER = 5;

  public static final byte LONG = 6;

  public static final byte FLOAT = 7;

  public static final byte DOUBLE = 8;

  public static final byte STRING = 9;

  public static void transmitValues(Channel channel, Object[] values)
  {
    for (int i = 0; i < values.length; i++)
    {
      Object value = values[i];

      if (value instanceof Boolean)
      {
        channel.transmitByte(BOOLEAN);
        channel.transmitBoolean(((Boolean) value).booleanValue());
      }
      else if (value instanceof Character)
      {
        channel.transmitByte(CHARACTER);
        channel.transmitChar(((Character) value).charValue());
      }
      else if (value instanceof Byte)
      {
        channel.transmitByte(BYTE);
        channel.transmitByte(((Byte) value).byteValue());
      }
      else if (value instanceof Short)
      {
        channel.transmitByte(SHORT);
        channel.transmitShort(((Short) value).shortValue());
      }
      else if (value instanceof Integer)
      {
        channel.transmitByte(INTEGER);
        channel.transmitInt(((Integer) value).intValue());
      }
      else if (value instanceof Long)
      {
        channel.transmitByte(LONG);
        channel.transmitLong(((Long) value).longValue());
      }
      else if (value instanceof Float)
      {
        channel.transmitByte(FLOAT);
        channel.transmitFloat(((Float) value).floatValue());
      }
      else if (value instanceof Double)
      {
        channel.transmitByte(DOUBLE);
        channel.transmitDouble(((Double) value).doubleValue());
      }
      else if (value instanceof String)
      {
        channel.transmitByte(STRING);
        channel.transmitString((String) value);
      }
      else
      {
        throw new ImplementationError("invalid class: " + value.getClass().getName());
      }
    }

    channel.transmitByte(END_OF_TRANSMISSION);
  }

  public static Object[] receiveValues(Channel channel)
  {
    List list = new ArrayList();

    for (;;)
    {
      byte type = channel.receiveByte();

      switch (type)
      {
        case END_OF_TRANSMISSION:
          return list.toArray();

        case BOOLEAN:
          list.add(new Boolean(channel.receiveBoolean()));
          break;

        case CHARACTER:
          list.add(new Character(channel.receiveChar()));
          break;

        case BYTE:
          list.add(new Byte(channel.receiveByte()));
          break;

        case SHORT:
          list.add(new Short(channel.receiveShort()));
          break;

        case INTEGER:
          list.add(new Integer(channel.receiveInt()));
          break;

        case LONG:
          list.add(new Long(channel.receiveLong()));
          break;

        case FLOAT:
          list.add(new Float(channel.receiveFloat()));
          break;

        case DOUBLE:
          list.add(new Double(channel.receiveDouble()));
          break;

        case STRING:
          list.add(channel.receiveString());
          break;
      }
    }
  }

  public static int sizeOf(Object[] values)
  {
    int sum = 4; // END_OF_TRANSMISSION
    for (int i = 0; i < values.length; i++)
    {
      Object value = values[i];

      if (value instanceof Boolean)
      {
        sum += 1 + 1;
      }
      else if (value instanceof Character)
      {
        sum += 1 + 2;
      }
      else if (value instanceof Byte)
      {
        sum += 1 + 1;
      }
      else if (value instanceof Short)
      {
        sum += 1 + 2;
      }
      else if (value instanceof Integer)
      {
        sum += 1 + 4;
      }
      else if (value instanceof Long)
      {
        sum += 1 + 8;
      }
      else if (value instanceof Float)
      {
        sum += 1 + 4;
      }
      else if (value instanceof Double)
      {
        sum += 1 + 8;
      }
      else if (value instanceof String)
      {
        sum += 1 + 4 + 2 * ((String) value).length();
      }
      else
      {
        throw new ImplementationError("invalid class: " + value.getClass().getName());
      }
    }

    return sum;
  }
}

Back to the top