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;
19 import com.google.zxing.Result;
20 import com.google.zxing.client.result.URIParsedResult;
23 * Recognizes an NDEF message that encodes a URI according to the
24 * "URI Record Type Definition" specification.
26 * @author srowen@google.com (Sean Owen)
28 public final class NDEFURIResultParser extends AbstractNDEFResultParser {
30 private static final String[] URI_PREFIXES = {
38 "ftp://anonymous:anonymous@",
69 public static URIParsedResult parse(Result result) {
70 byte[] bytes = result.getRawBytes();
74 NDEFRecord ndefRecord = NDEFRecord.readRecord(bytes, 0);
75 if (ndefRecord == null || !ndefRecord.isMessageBegin() || !ndefRecord.isMessageEnd()) {
78 if (!ndefRecord.getType().equals(NDEFRecord.URI_WELL_KNOWN_TYPE)) {
81 String fullURI = decodeURIPayload(ndefRecord.getPayload());
82 return new URIParsedResult(fullURI, null);
85 static String decodeURIPayload(byte[] payload) {
86 int identifierCode = payload[0] & 0xFF;
88 if (identifierCode < URI_PREFIXES.length) {
89 prefix = URI_PREFIXES[identifierCode];
91 String restOfURI = bytesToString(payload, 1, payload.length - 1, "UTF8");
92 return prefix == null ? restOfURI : prefix + restOfURI;