All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.typeconverter.impl.FileConverter Maven / Gradle / Ivy

Go to download

Jodd Core tools and utilities, including type converters, JDateTime, cache etc.

There is a newer version: 5.3.0
Show newest version
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.typeconverter.impl;

import jodd.io.FileUtil;
import jodd.typeconverter.TypeConversionException;
import jodd.typeconverter.TypeConverter;
import jodd.util.ArraysUtil;

import java.io.File;
import java.io.IOException;

/**
 * Converts given object into the File.
 * If created, returned file is stored in the temporary folder.
 * Conversion rules:
 * 
    *
  • null value is returned as null
  • *
  • object of destination type is simply casted
  • *
  • byte[] content is used for creating a file
  • *
  • String content is used for creating a file
  • *
*

* This converter is plugable and add-on file converters from * other modules can be added. */ public class FileConverter implements TypeConverter { protected TypeConverter[] addonFileConverters; @SuppressWarnings("unchecked") public void registerAddonConverter(TypeConverter fileTypeConverter) { if (addonFileConverters == null) { addonFileConverters = new TypeConverter[0]; } ArraysUtil.append(addonFileConverters, fileTypeConverter); } public File convert(Object value) { if (value == null) { return null; } if (value instanceof File) { return (File) value; } if (addonFileConverters != null) { for (TypeConverter addonFileConverter : addonFileConverters) { File file = addonFileConverter.convert(value); if (file != null) { return file; } } } /* if (value instanceof FileUpload) { FileUpload fileUpload = (FileUpload) value; InputStream in = null; try { in = fileUpload.getFileInputStream(); File tempFile = FileUtil.createTempFile(); FileUtil.writeStream(tempFile, in); return tempFile; } catch (IOException ioex) { throw new TypeConversionException(ioex); } finally { StreamUtil.close(in); } } */ Class type = value.getClass(); if (type == byte[].class) { try { File tempFile = FileUtil.createTempFile(); FileUtil.writeBytes(tempFile, (byte[])value); return tempFile; } catch (IOException ioex) { throw new TypeConversionException(ioex); } } if (type == String.class) { try { File tempFile = FileUtil.createTempFile(); FileUtil.writeString(tempFile, value.toString()); return tempFile; } catch (IOException ioex) { throw new TypeConversionException(ioex); } } throw new TypeConversionException(value); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy