1 /* 2 * Copyright (C) 2014 The Android Open Source Project 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.android.bluetooth.mapclient; 18 19 import java.io.IOException; 20 import java.nio.ByteBuffer; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 import javax.obex.HeaderSet; 25 26 public final class ObexAppParameters { 27 28 private final HashMap<Byte, byte[]> mParams; 29 ObexAppParameters()30 public ObexAppParameters() { 31 mParams = new HashMap<Byte, byte[]>(); 32 } 33 ObexAppParameters(byte[] raw)34 public ObexAppParameters(byte[] raw) { 35 mParams = new HashMap<Byte, byte[]>(); 36 37 if (raw != null) { 38 for (int i = 0; i < raw.length; ) { 39 if (raw.length - i < 2) { 40 break; 41 } 42 43 byte tag = raw[i++]; 44 byte len = raw[i++]; 45 46 if (raw.length - i - len < 0) { 47 break; 48 } 49 50 byte[] val = new byte[len]; 51 52 System.arraycopy(raw, i, val, 0, len); 53 this.add(tag, val); 54 55 i += len; 56 } 57 } 58 } 59 fromHeaderSet(HeaderSet headerset)60 public static ObexAppParameters fromHeaderSet(HeaderSet headerset) { 61 try { 62 byte[] raw = (byte[]) headerset.getHeader(HeaderSet.APPLICATION_PARAMETER); 63 return new ObexAppParameters(raw); 64 } catch (IOException e) { 65 // won't happen 66 } 67 68 return null; 69 } 70 getHeader()71 public byte[] getHeader() { 72 int length = 0; 73 74 for (Map.Entry<Byte, byte[]> entry : mParams.entrySet()) { 75 length += (entry.getValue().length + 2); 76 } 77 78 byte[] ret = new byte[length]; 79 80 int idx = 0; 81 for (Map.Entry<Byte, byte[]> entry : mParams.entrySet()) { 82 length = entry.getValue().length; 83 84 ret[idx++] = entry.getKey(); 85 ret[idx++] = (byte) length; 86 System.arraycopy(entry.getValue(), 0, ret, idx, length); 87 idx += length; 88 } 89 90 return ret; 91 } 92 addToHeaderSet(HeaderSet headerset)93 public void addToHeaderSet(HeaderSet headerset) { 94 if (mParams.size() > 0) { 95 headerset.setHeader(HeaderSet.APPLICATION_PARAMETER, getHeader()); 96 } 97 } 98 exists(byte tag)99 public boolean exists(byte tag) { 100 return mParams.containsKey(tag); 101 } 102 add(byte tag, byte val)103 public void add(byte tag, byte val) { 104 byte[] bval = ByteBuffer.allocate(1).put(val).array(); 105 mParams.put(tag, bval); 106 } 107 add(byte tag, short val)108 public void add(byte tag, short val) { 109 byte[] bval = ByteBuffer.allocate(2).putShort(val).array(); 110 mParams.put(tag, bval); 111 } 112 add(byte tag, int val)113 public void add(byte tag, int val) { 114 byte[] bval = ByteBuffer.allocate(4).putInt(val).array(); 115 mParams.put(tag, bval); 116 } 117 add(byte tag, long val)118 public void add(byte tag, long val) { 119 byte[] bval = ByteBuffer.allocate(8).putLong(val).array(); 120 mParams.put(tag, bval); 121 } 122 add(byte tag, String val)123 public void add(byte tag, String val) { 124 byte[] bval = val.getBytes(); 125 mParams.put(tag, bval); 126 } 127 add(byte tag, byte[] bval)128 public void add(byte tag, byte[] bval) { 129 mParams.put(tag, bval); 130 } 131 getByte(byte tag)132 public byte getByte(byte tag) { 133 byte[] bval = mParams.get(tag); 134 135 if (bval == null || bval.length < 1) { 136 return 0; 137 } 138 139 return ByteBuffer.wrap(bval).get(); 140 } 141 getShort(byte tag)142 public short getShort(byte tag) { 143 byte[] bval = mParams.get(tag); 144 145 if (bval == null || bval.length < 2) { 146 return 0; 147 } 148 149 return ByteBuffer.wrap(bval).getShort(); 150 } 151 getInt(byte tag)152 public int getInt(byte tag) { 153 byte[] bval = mParams.get(tag); 154 155 if (bval == null || bval.length < 4) { 156 return 0; 157 } 158 159 return ByteBuffer.wrap(bval).getInt(); 160 } 161 getString(byte tag)162 public String getString(byte tag) { 163 byte[] bval = mParams.get(tag); 164 165 if (bval == null) { 166 return null; 167 } 168 169 return new String(bval); 170 } 171 getByteArray(byte tag)172 public byte[] getByteArray(byte tag) { 173 byte[] bval = mParams.get(tag); 174 175 return bval; 176 } 177 178 @Override toString()179 public String toString() { 180 return mParams.toString(); 181 } 182 } 183