2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result.optional;
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>
24 * @author srowen@google.com (Sean Owen)
26 final class NDEFRecord {
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
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";
36 private final int header;
37 private final String type;
38 private final byte[] payload;
39 private final int totalRecordLength;
41 private NDEFRecord(int header, String type, byte[] payload, int totalRecordLength) {
44 this.payload = payload;
45 this.totalRecordLength = totalRecordLength;
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) {
55 int typeLength = bytes[offset + 1] & 0xFF;
57 int payloadLength = bytes[offset + 2] & 0xFF;
59 String type = AbstractNDEFResultParser.bytesToString(bytes, offset + 3, typeLength, "US-ASCII");
61 byte[] payload = new byte[payloadLength];
62 System.arraycopy(bytes, offset + 3 + typeLength, payload, 0, payloadLength);
64 return new NDEFRecord(header, type, payload, 3 + typeLength + payloadLength);
67 boolean isMessageBegin() {
68 return (header & 0x80) != 0;
71 boolean isMessageEnd() {
72 return (header & 0x40) != 0;
83 int getTotalRecordLength() {
84 return totalRecordLength;