001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 019package org.apache.commons.beanutils.converters; 020 021 022import java.util.List; 023import org.apache.commons.beanutils.ConversionException; 024 025 026/** 027 * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming 028 * String into a primitive array of byte. On a conversion failure, returns 029 * a specified default value or throws a {@link ConversionException} depending 030 * on how this instance is constructed.</p> 031 * 032 * @version $Id$ 033 * @since 1.4 034 * @deprecated Replaced by the new {@link ArrayConverter} implementation 035 */ 036 037@Deprecated 038public final class ByteArrayConverter extends AbstractArrayConverter { 039 040 041 // ----------------------------------------------------------- Constructors 042 043 044 /** 045 * Create a {@link org.apache.commons.beanutils.Converter} that will 046 * throw a {@link ConversionException} if a conversion error occurs. 047 */ 048 public ByteArrayConverter() { 049 050 this.defaultValue = null; 051 this.useDefault = false; 052 053 } 054 055 056 /** 057 * Create a {@link org.apache.commons.beanutils.Converter} that will return 058 * the specified default value if a conversion error occurs. 059 * 060 * @param defaultValue The default value to be returned 061 */ 062 public ByteArrayConverter(final Object defaultValue) { 063 064 this.defaultValue = defaultValue; 065 this.useDefault = true; 066 067 } 068 069 070 // ------------------------------------------------------- Static Variables 071 072 073 /** 074 * <p>Model object for type comparisons.</p> 075 */ 076 private static final byte[] MODEL = new byte[0]; 077 078 079 // --------------------------------------------------------- Public Methods 080 081 082 /** 083 * Convert the specified input object into an output object of the 084 * specified type. 085 * 086 * @param type Data type to which this value should be converted 087 * @param value The input value to be converted 088 * @return the converted value 089 * @throws ConversionException if conversion cannot be performed 090 * successfully 091 */ 092 @Override 093 public Object convert(final Class type, final Object value) { 094 095 // Deal with a null value 096 if (value == null) { 097 if (useDefault) { 098 return (defaultValue); 099 } else { 100 throw new ConversionException("No value specified"); 101 } 102 } 103 104 // Deal with the no-conversion-needed case 105 if (MODEL.getClass() == value.getClass()) { 106 return (value); 107 } 108 109 // Deal with input value as a String array 110 if (strings.getClass() == value.getClass()) { 111 try { 112 final String[] values = (String[]) value; 113 final byte[] results = new byte[values.length]; 114 for (int i = 0; i < values.length; i++) { 115 results[i] = Byte.parseByte(values[i]); 116 } 117 return (results); 118 } catch (final Exception e) { 119 if (useDefault) { 120 return (defaultValue); 121 } else { 122 throw new ConversionException(value.toString(), e); 123 } 124 } 125 } 126 127 // Parse the input value as a String into elements 128 // and convert to the appropriate type 129 try { 130 final List list = parseElements(value.toString()); 131 final byte[] results = new byte[list.size()]; 132 for (int i = 0; i < results.length; i++) { 133 results[i] = Byte.parseByte((String) list.get(i)); 134 } 135 return (results); 136 } catch (final Exception e) { 137 if (useDefault) { 138 return (defaultValue); 139 } else { 140 throw new ConversionException(value.toString(), e); 141 } 142 } 143 144 } 145 146 147}