Home | History | Annotate | Download | only in utils
      1 /**
      2  * Copyright (c) 2015 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 com.android.phone.common.mail.utils;
     17 
     18 import java.io.ByteArrayInputStream;
     19 import java.nio.ByteBuffer;
     20 import java.nio.CharBuffer;
     21 import java.nio.charset.Charset;
     22 
     23 /**
     24  * Simple utility methods used in email functions.
     25  */
     26 public class Utility {
     27     public static final Charset ASCII = Charset.forName("US-ASCII");
     28 
     29     public static final String[] EMPTY_STRINGS = new String[0];
     30 
     31     /**
     32      * Returns a concatenated string containing the output of every Object's
     33      * toString() method, each separated by the given separator character.
     34      */
     35     public static String combine(Object[] parts, char separator) {
     36         if (parts == null) {
     37             return null;
     38         }
     39         StringBuilder sb = new StringBuilder();
     40         for (int i = 0; i < parts.length; i++) {
     41             sb.append(parts[i].toString());
     42             if (i < parts.length - 1) {
     43                 sb.append(separator);
     44             }
     45         }
     46         return sb.toString();
     47     }
     48 
     49     /** Converts a String to ASCII bytes */
     50     public static byte[] toAscii(String s) {
     51         return encode(ASCII, s);
     52     }
     53 
     54     /** Builds a String from ASCII bytes */
     55     public static String fromAscii(byte[] b) {
     56         return decode(ASCII, b);
     57     }
     58 
     59     private static byte[] encode(Charset charset, String s) {
     60         if (s == null) {
     61             return null;
     62         }
     63         final ByteBuffer buffer = charset.encode(CharBuffer.wrap(s));
     64         final byte[] bytes = new byte[buffer.limit()];
     65         buffer.get(bytes);
     66         return bytes;
     67     }
     68 
     69     private static String decode(Charset charset, byte[] b) {
     70         if (b == null) {
     71             return null;
     72         }
     73         final CharBuffer cb = charset.decode(ByteBuffer.wrap(b));
     74         return new String(cb.array(), 0, cb.length());
     75     }
     76 
     77     public static ByteArrayInputStream streamFromAsciiString(String ascii) {
     78         return new ByteArrayInputStream(toAscii(ascii));
     79     }
     80 }
     81