26f6867e3ef1e3a35a1f827773f0c3c7e8062996
[mediacube.git] /
1 package user.commons.nexio.server.protocol;
2
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.nio.ByteOrder;
6 import java.util.Calendar;
7
8 public class GetExtendedFieldGetModifiedTimestampCommand extends Command {
9
10         private final static byte[] GET_EXTENDED_FIELD = { (byte) 0xc9, (byte) 0xc3 };
11         private final static byte[] EXTENDED_FIELD_NUMBER = { (byte) 20 };
12
13         private byte[] buffer;
14         private byte filetime[];
15
16         public GetExtendedFieldGetModifiedTimestampCommand(Connection connection) {
17                 super(connection);
18         }
19
20         private Calendar convertFromFILETIME(byte[] filetime) {
21                 Calendar result = Calendar.getInstance();
22
23                 ByteBuffer b = ByteBuffer.wrap(filetime);
24                 b.order(ByteOrder.LITTLE_ENDIAN);
25                 int lo = b.asIntBuffer().get(0);
26                 int hi = b.asIntBuffer().get(1);
27
28                 long wFILETIME = ((long) hi << 32) + lo;
29
30                 final long EPOCH_DIFF = 11644473600000L;
31                 final long ms_since_16010101 = wFILETIME / (10000);
32                 final long ms_since_19700101 = ms_since_16010101 - EPOCH_DIFF;
33
34                 result.setTimeInMillis(ms_since_19700101);
35
36                 return result;
37         }
38
39         public Calendar execute(Id id) throws IOException, ProtocolException {
40
41                 Calendar ret = null;
42
43                 connection.write(GET_EXTENDED_FIELD);
44                 connection.write(id.get().getBytes());
45                 connection.write(EXTENDED_FIELD_NUMBER);
46                 connection.flush();
47
48                 buffer = new byte[2];
49                 int c = connection.read(buffer, 0, 2);
50                 if (c < 2) {
51                         throw getException_InvalidResponseLength(c, 2, 2);
52                 }
53
54                 // Not found
55                 if (buffer[0] == (byte) 0xd0 && buffer[1] == (byte) 0xc3) {
56                         throw new ProtocolException("ID handle is not found!");
57                 } else
58                 // 1. Found
59                 if (buffer[0] == (byte) 0xdf && buffer[1] == (byte) 0xc3) {
60
61                         // 2. read NOF BYTES
62                         buffer = new byte[1];
63                         c = connection.read(buffer, 0, 1);
64
65                         int toRead = buffer[0];
66                         if (toRead != 8) {
67                                 throw new ProtocolException("Invalid ModifiedTimeStamp's data length: " + toRead);
68                         } else {
69                                 filetime = new byte[8];
70                                 c = connection.read(filetime, 0, 8);
71                                 if (c != 8) {
72                                         throw getException_InvalidResponseLength(c, 8, 8);
73                                 }
74                                 ret = convertFromFILETIME(filetime);
75                         }
76                 }
77                 return ret;
78         }
79 }