e92161d14ddc9b6b8b081f1599cbdb6dca6e91f5
[zxing.git] / core / src / com / google / zxing / client / result / optional / NDEFRecord.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result.optional;
18
19 /**
20  * <p>Represents a record in an NDEF message. This class only supports certain types
21  * of records -- namely, non-chunked records, where ID length is omitted, and only
22  * "short records".</p>
23  *
24  * @author srowen@google.com (Sean Owen)
25  */
26 final class NDEFRecord {
27
28   private static final int SUPPORTED_HEADER_MASK = 0x3F; // 0 0 1 1 1 111 (the bottom 6 bits matter)
29   private static final int SUPPORTED_HEADER = 0x11;      // 0 0 0 1 0 001
30
31   public static final String TEXT_WELL_KNOWN_TYPE = "T";
32   public static final String URI_WELL_KNOWN_TYPE = "U";
33   public static final String SMART_POSTER_WELL_KNOWN_TYPE = "Sp";
34   public static final String ACTION_WELL_KNOWN_TYPE = "act";
35
36   private final int header;
37   private final String type;
38   private final byte[] payload;
39   private final int totalRecordLength;
40
41   private NDEFRecord(int header, String type, byte[] payload, int totalRecordLength) {
42     this.header = header;
43     this.type = type;
44     this.payload = payload;
45     this.totalRecordLength = totalRecordLength;
46   }
47
48   static NDEFRecord readRecord(byte[] bytes, int offset) {
49     int header = bytes[offset] & 0xFF;
50     // Does header match what we support in the bits we care about?
51     // XOR figures out where we differ, and if any of those are in the mask, fail
52     if (((header ^ SUPPORTED_HEADER) & SUPPORTED_HEADER_MASK) != 0) {
53       return null;
54     }
55     int typeLength = bytes[offset + 1] & 0xFF;
56
57     int payloadLength = bytes[offset + 2] & 0xFF;
58
59     String type = AbstractNDEFResultParser.bytesToString(bytes, offset + 3, typeLength, "US-ASCII");
60
61     byte[] payload = new byte[payloadLength];
62     System.arraycopy(bytes, offset + 3 + typeLength, payload, 0, payloadLength);
63
64     return new NDEFRecord(header, type, payload, 3 + typeLength + payloadLength);
65   }
66
67   boolean isMessageBegin() {
68     return (header & 0x80) != 0;
69   }
70
71   boolean isMessageEnd() {
72     return (header & 0x40) != 0;
73   }
74
75   String getType() {
76     return type;
77   }
78
79   byte[] getPayload() {
80     return payload;
81   }
82
83   int getTotalRecordLength() {
84     return totalRecordLength;
85   }
86
87 }