26c6723e73a94e64c9d6561496e6f6b65a2c6faf
[mediacube.git] /
1 package user.commons.nexio.server.protocol;
2
3 import java.io.IOException;
4
5 public class GetExtendedIDFromIDHandleCommand extends Command {
6
7         private static final String INVALID_RESPONSE = "Invalid response length, response should be 2-258 characters long, but 1 got";
8         private final static byte[] GET_EXTENDED_ID_FROM_ID_HANDLE = { (byte) 0xc8, (byte) 0xc3 };
9         private byte[] buffer;
10
11         public GetExtendedIDFromIDHandleCommand(Connection connection) {
12                 super(connection);
13         }
14
15         public Xid execute(Id id) throws IOException, ProtocolException {
16
17                 Xid ret = null;
18
19                 connection.write(GET_EXTENDED_ID_FROM_ID_HANDLE);
20                 connection.write(id.get().getBytes());
21                 connection.flush();
22
23                 buffer = new byte[2];
24                 int c = connection.read(buffer, 0, 2);
25                 if (c < 2) {
26                         throw new ProtocolException(INVALID_RESPONSE);
27                 }
28
29                 // Not found
30                 if (buffer[0] == (byte) 0xd0 && buffer[1] == (byte) 0xc3) {
31                         throw new ProtocolException("ID handle is not found!");
32                 }
33
34                 // 1. Found
35                 if (buffer[0] == (byte) 0xdf && buffer[1] == (byte) 0xc3) {
36
37                         // 2. read NOF BYTES
38                         buffer = new byte[1];
39                         c = connection.read(buffer, 0, 1);
40
41                         // 3. read data
42                         int toRead = buffer[0];
43                         buffer = new byte[toRead];
44                         c = connection.read(buffer, 0, toRead);
45
46                         if (c != toRead) {
47                                 throw getException_InvalidXIDLength(toRead, new String(buffer));
48                         }
49
50                         ret = new Xid(new String(buffer));
51                 }
52
53                 return ret;
54         }
55
56 }