de.drop_converter.plugins.binary_convert.Base642Data.java Source code

Java tutorial

Introduction

Here is the source code for de.drop_converter.plugins.binary_convert.Base642Data.java

Source

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package de.drop_converter.plugins.binary_convert;

import java.awt.datatransfer.DataFlavor;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

import javax.swing.TransferHandler.TransferSupport;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.io.IOUtils;

import de.drop_converter.plugin.annotations.ConverterPluginDetails;
import de.drop_converter.plugin.exception.ConverterException;

/**
 * Convert base64 to data.
 * 
 * @author Thomas Chojecki
 */
@ConverterPluginDetails(authorEmail = "info@rayman2200.de", authorName = "Thomas Chojecki", pluginDescription = "Convert base64 to data", pluginName = "Base642Data", pluginVersion = "1.0.0")
public class Base642Data extends AbstractDataPlugin {

    @Override
    public boolean importData(TransferSupport support) throws ConverterException {
        try {
            if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                List<File> files = (List<File>) support.getTransferable()
                        .getTransferData(DataFlavor.javaFileListFlavor);
                for (File file : files) {
                    FileInputStream fis = null;
                    Base64InputStream base64IS = null;
                    OutputStream out = null;
                    try {
                        fis = new FileInputStream(file);
                        base64IS = new Base64InputStream(fis, false);
                        out = getOutputStream(file, ".bin");
                        IOUtils.copy(base64IS, out);
                    } catch (Exception e) {
                        throw new ConverterException(e);
                    } finally {
                        IOUtils.closeQuietly(out);
                        IOUtils.closeQuietly(base64IS);
                        IOUtils.closeQuietly(fis);
                    }
                }
                return true;
            } else if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
                OutputStream out = null;
                try {
                    byte[] encode = new Base64().decode(data.getBytes());
                    out = getOutputStream(null, ".bin");
                    out.write(encode);
                } catch (Exception e) {
                    throw new ConverterException(e);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        } catch (Exception e) {
            throw new ConverterException(e);
        }

        return false;
    }
}