    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
    018package org.apache.commons.beanutils.converters;
    019
    020import java.util.List;
    021
    022import org.apache.commons.beanutils.ConversionException;
    023
    024/**
    025 * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
    026 * String into a primitive array of byte.  On a conversion failure, returns
    027 * a specified default value or throws a {@link ConversionException} depending
    028 * on how this instance is constructed.</p>
    029 *
    030 * @since 1.4
    031 * @deprecated Replaced by the new {@link ArrayConverter} implementation
    032 */
    033
    034@Deprecated
    035public final class ByteArrayConverter extends AbstractArrayConverter {
    036
    037    /**
    038     * <p>Model object for type comparisons.</p>
    039     */
    040    private static final byte[] MODEL = {};
    041
    042    /**
    043     * Create a {@link org.apache.commons.beanutils.Converter} that will
    044     * throw a {@link ConversionException} if a conversion error occurs.
    045     */
    046    public ByteArrayConverter() {
    047
    048        this.defaultValue = null;
    049        this.useDefault = false;
    050
    051    }
    052
    053    /**
    054     * Create a {@link org.apache.commons.beanutils.Converter} that will return
    055     * the specified default value if a conversion error occurs.
    056     *
    057     * @param defaultValue The default value to be returned
    058     */
    059    public ByteArrayConverter(final Object defaultValue) {
    060
    061        this.defaultValue = defaultValue;
    062        this.useDefault = true;
    063
    064    }
    065
    066    /**
    067     * Convert the specified input object into an output object of the
    068     * specified type.
    069     *
    070     * @param type Data type to which this value should be converted
    071     * @param value The input value to be converted
    072     * @return the converted value
    073     * @throws ConversionException if conversion cannot be performed
    074     *  successfully
    075     */
    076    @Override
    077    public Object convert(final Class type, final Object value) {
    078
    079        // Deal with a null value
    080        if (value == null) {
    081            if (useDefault) {
    082                return defaultValue;
    083            }
    084            throw new ConversionException("No value specified");
    085        }
    086
    087        // Deal with the no-conversion-needed case
    088        if (MODEL.getClass() == value.getClass()) {
    089            return value;
    090        }
    091
    092        // Deal with input value as a String array
    093        if (strings.getClass() == value.getClass()) {
    094            try {
    095                final String[] values = (String[]) value;
    096                final byte[] results = new byte[values.length];
    097                for (int i = 0; i < values.length; i++) {
    098                    results[i] = Byte.parseByte(values[i]);
    099                }
    100                return results;
    101            } catch (final Exception e) {
    102                if (useDefault) {
    103                    return defaultValue;
    104                }
    105                throw new ConversionException(value.toString(), e);
    106            }
    107        }
    108
    109        // Parse the input value as a String into elements
    110        // and convert to the appropriate type
    111        try {
    112            final List<String> list = parseElements(value.toString());
    113            final byte[] results = new byte[list.size()];
    114            for (int i = 0; i < results.length; i++) {
    115                results[i] = Byte.parseByte(list.get(i));
    116            }
    117            return results;
    118        } catch (final Exception e) {
    119            if (useDefault) {
    120                return defaultValue;
    121            }
    122            throw new ConversionException(value.toString(), e);
    123        }
    124
    125    }
    126
    127}