| package org.eclipse.osbp.vaaclipse.addons.app.converter; |
| |
| import java.util.Base64; |
| import java.util.Locale; |
| |
| import com.vaadin.data.util.converter.Converter; |
| |
| @SuppressWarnings("serial") |
| public class VaaclipseByteArrayConverter implements Converter<String, byte[]> { |
| |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, |
| * java.lang.Class, java.util.Locale) |
| */ |
| @Override |
| public byte[] convertToModel(String value, Class<? extends byte[]> targetType, Locale locale) |
| throws com.vaadin.data.util.converter.Converter.ConversionException { |
| if (targetType != getModelType()) { |
| throw new ConversionException("Converter only supports " + getModelType().getName() + " (targetType was " |
| + targetType.getName() + ")"); |
| } |
| |
| if (value == null) { |
| return null; |
| } |
| |
| // Remove leading and trailing white space |
| value = value.trim(); |
| |
| return Base64.getEncoder().encode(value.getBytes()); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang |
| * .Object, java.lang.Class, java.util.Locale) |
| */ |
| @Override |
| public String convertToPresentation(byte[] value, Class<? extends String> targetType, Locale locale) |
| throws com.vaadin.data.util.converter.Converter.ConversionException { |
| if (value == null) { |
| return null; |
| } |
| |
| return new String(Base64.getDecoder().decode(value)); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.data.util.converter.Converter#getModelType() |
| */ |
| @Override |
| public Class<byte[]> getModelType() { |
| return byte[].class; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.data.util.converter.Converter#getPresentationType() |
| */ |
| @Override |
| public Class<String> getPresentationType() { |
| return String.class; |
| } |
| |
| } |