Home | History | Annotate | Download | only in impl
      1 /*
      2  * Copyright (C) 2013 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 package android.hardware.camera2.impl;
     17 
     18 import java.nio.ByteBuffer;
     19 import java.nio.charset.Charset;
     20 
     21 public class MetadataMarshalString implements MetadataMarshalClass<String> {
     22 
     23     private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
     24 
     25     @Override
     26     public int marshal(String value, ByteBuffer buffer, int nativeType, boolean sizeOnly) {
     27         byte[] arr = value.getBytes(UTF8_CHARSET);
     28 
     29         if (!sizeOnly) {
     30             buffer.put(arr);
     31             buffer.put((byte)0); // metadata strings are NULL-terminated
     32         }
     33 
     34         return arr.length + 1;
     35     }
     36 
     37     @Override
     38     public String unmarshal(ByteBuffer buffer, int nativeType) {
     39 
     40         buffer.mark(); // save the current position
     41 
     42         boolean foundNull = false;
     43         int stringLength = 0;
     44         while (buffer.hasRemaining()) {
     45             if (buffer.get() == (byte)0) {
     46                 foundNull = true;
     47                 break;
     48             }
     49 
     50             stringLength++;
     51         }
     52         if (!foundNull) {
     53             throw new IllegalArgumentException("Strings must be null-terminated");
     54         }
     55 
     56         buffer.reset(); // go back to the previously marked position
     57 
     58         byte[] strBytes = new byte[stringLength + 1];
     59         buffer.get(strBytes, /*dstOffset*/0, stringLength + 1); // including null character
     60 
     61         // not including null character
     62         return new String(strBytes, /*offset*/0, stringLength, UTF8_CHARSET);
     63     }
     64 
     65     @Override
     66     public Class<String> getMarshalingClass() {
     67         return String.class;
     68     }
     69 
     70     @Override
     71     public boolean isNativeTypeSupported(int nativeType) {
     72         return nativeType == CameraMetadataNative.TYPE_BYTE;
     73     }
     74 
     75     @Override
     76     public int getNativeSize(int nativeType) {
     77         return NATIVE_SIZE_DYNAMIC;
     78     }
     79 }
     80