001package org.unix4j.convert;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.IOException;
006import java.io.InputStream;
007import java.net.URL;
008
009public class InputStreamConverters {
010        public static final ValueConverter<InputStream> URL_TO_STREAM = new ValueConverter<InputStream>() {
011                @Override
012                public InputStream convert(Object value) throws IllegalArgumentException {
013                        if (value instanceof URL) {
014                                try {
015                                        return ((URL)value).openStream();
016                                } catch (IOException e) {
017                                        return null;
018                                }
019                        }
020                        return null;
021                }
022        };
023        public static final ValueConverter<InputStream> URL = ConcatenatedConverter.concat(URLConverters.DEFAULT, URL_TO_STREAM);
024        
025        public static final ValueConverter<InputStream> FILE_TO_STREAM = new ValueConverter<InputStream>() {
026                @Override
027                public InputStream convert(Object value) throws IllegalArgumentException {
028                        if (value instanceof File) {
029                                try {
030                                        return new FileInputStream((File)value);
031                                } catch (IOException e) {
032                                        return null;
033                                }
034                        }
035                        return null;
036                }
037        };
038        public static final ValueConverter<InputStream> FILE = ConcatenatedConverter.concat(FileConverters.DEFAULT, FILE_TO_STREAM);
039        
040        public static final ValueConverter<InputStream> DEFAULT = new CompositeValueConverter<InputStream>().add(URL).add(FILE);
041}