Java Reference
In-Depth Information
6
7
public ItemQuoteDecoderText() {
8
encoding = DEFAULT_ENCODING;
9
}
10
11
public ItemQuoteDecoderText(String encoding) {
12
this.encoding = encoding;
13
}
14
15
public ItemQuote decode(InputStream wire) throws IOException {
16
String itemNo, description, quant, price, flags;
17
byte[] space = " ".getBytes(encoding);
18
byte[] newline = "\n".getBytes(encoding);
19
itemNo = new String(Framer.nextToken(wire, space), encoding);
20
description = new String(Framer.nextToken(wire, newline), encoding);
21
quant = new String(Framer.nextToken(wire, space), encoding);
22
price = new String(Framer.nextToken(wire, space), encoding);
23
flags = new String(Framer.nextToken(wire, newline), encoding);
24
return new ItemQuote(Long.parseLong(itemNo), description,
25
Integer.parseInt(quant),
26
Integer.parseInt(price),
27
(flags.indexOf('d') != −1),
28
(flags.indexOf('s') != −1));
29
}
30
31
public ItemQuote decode(DatagramPacket p) throws IOException {
32
ByteArrayInputStream payload =
33
new ByteArrayInputStream(p.getData(), p.getOffset(), p.getLength());
34
return decode(payload);
35
}
36 }
ItemQuoteDecoderText.java
1. Variables and constructors: lines 5-13
Encoding: line 5
The encoding used in the decoder must be the same as in the encoder!
Constructors: lines 7-13
If no encoding is given at construction time, the default defined in ItemQuoteDecoder-
TextConst is used.
2. Stream decode() : lines 15-29
Convert delimiters: lines 17-18
We get the encoded form of the delimiters ahead of time, for eciency.
Search WWH ::




Custom Search