Coverage Report - org.apache.commons.beanutils.converters.ByteArrayConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ByteArrayConverter
0%
0/34
0%
0/16
7.333
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 
 19  
 package org.apache.commons.beanutils.converters;
 20  
 
 21  
 
 22  
 import java.util.List;
 23  
 import org.apache.commons.beanutils.ConversionException;
 24  
 
 25  
 
 26  
 /**
 27  
  * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
 28  
  * String into a primitive array of byte.  On a conversion failure, returns
 29  
  * a specified default value or throws a {@link ConversionException} depending
 30  
  * on how this instance is constructed.</p>
 31  
  *
 32  
  * @author Craig R. McClanahan
 33  
  * @version $Revision: 556229 $ $Date: 2007-07-14 02:11:19 -0400 (Sat, 14 Jul 2007) $
 34  
  * @since 1.4
 35  
  * @deprecated Replaced by the new {@link ArrayConverter} implementation
 36  
  */
 37  
 
 38  
 public final class ByteArrayConverter extends AbstractArrayConverter {
 39  
 
 40  
 
 41  
     // ----------------------------------------------------------- Constructors
 42  
 
 43  
 
 44  
     /**
 45  
      * Create a {@link org.apache.commons.beanutils.Converter} that will
 46  
      * throw a {@link ConversionException} if a conversion error occurs.
 47  
      */
 48  0
     public ByteArrayConverter() {
 49  
 
 50  0
         this.defaultValue = null;
 51  0
         this.useDefault = false;
 52  
 
 53  0
     }
 54  
 
 55  
 
 56  
     /**
 57  
      * Create a {@link org.apache.commons.beanutils.Converter} that will return
 58  
      * the specified default value if a conversion error occurs.
 59  
      *
 60  
      * @param defaultValue The default value to be returned
 61  
      */
 62  0
     public ByteArrayConverter(Object defaultValue) {
 63  
 
 64  0
         this.defaultValue = defaultValue;
 65  0
         this.useDefault = true;
 66  
 
 67  0
     }
 68  
 
 69  
 
 70  
     // ------------------------------------------------------- Static Variables
 71  
 
 72  
 
 73  
     /**
 74  
      * <p>Model object for type comparisons.</p>
 75  
      */
 76  0
     private static final byte[] MODEL = new byte[0];
 77  
 
 78  
 
 79  
     // --------------------------------------------------------- Public Methods
 80  
 
 81  
 
 82  
     /**
 83  
      * Convert the specified input object into an output object of the
 84  
      * specified type.
 85  
      *
 86  
      * @param type Data type to which this value should be converted
 87  
      * @param value The input value to be converted
 88  
      * @return the converted value
 89  
      * @exception ConversionException if conversion cannot be performed
 90  
      *  successfully
 91  
      */
 92  
     public Object convert(Class type, Object value) {
 93  
 
 94  
         // Deal with a null value
 95  0
         if (value == null) {
 96  0
             if (useDefault) {
 97  0
                 return (defaultValue);
 98  
             } else {
 99  0
                 throw new ConversionException("No value specified");
 100  
             }
 101  
         }
 102  
 
 103  
         // Deal with the no-conversion-needed case
 104  0
         if (MODEL.getClass() == value.getClass()) {
 105  0
             return (value);
 106  
         }
 107  
 
 108  
         // Deal with input value as a String array
 109  0
         if (strings.getClass() == value.getClass()) {
 110  
             try {
 111  0
                 String[] values = (String[]) value;
 112  0
                 byte[] results = new byte[values.length];
 113  0
                 for (int i = 0; i < values.length; i++) {
 114  0
                     results[i] = Byte.parseByte(values[i]);
 115  
                 }
 116  0
                 return (results);
 117  0
             } catch (Exception e) {
 118  0
                 if (useDefault) {
 119  0
                     return (defaultValue);
 120  
                 } else {
 121  0
                     throw new ConversionException(value.toString(), e);
 122  
                 }
 123  
             }
 124  
         }
 125  
 
 126  
         // Parse the input value as a String into elements
 127  
         // and convert to the appropriate type
 128  
         try {
 129  0
             List list = parseElements(value.toString());
 130  0
             byte[] results = new byte[list.size()];
 131  0
             for (int i = 0; i < results.length; i++) {
 132  0
                 results[i] = Byte.parseByte((String) list.get(i));
 133  
             }
 134  0
             return (results);
 135  0
         } catch (Exception e) {
 136  0
             if (useDefault) {
 137  0
                 return (defaultValue);
 138  
             } else {
 139  0
                 throw new ConversionException(value.toString(), e);
 140  
             }
 141  
         }
 142  
 
 143  
     }
 144  
 
 145  
 
 146  
 }