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