001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.util;
020
021import java.io.File;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.io.OutputStream;
027import java.io.Reader;
028
029/**
030 * File is deleted on JVM exit. You should delete it explicitly earlier if you know it won't be used anymore.
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class IOUtils {
035
036    private static final int BUFFER_SIZE = 1024 * 64; // 64K
037
038    private static final int MAX_BUFFER_SIZE = 1024 * 1024; // 64K
039
040    private static final int MIN_BUFFER_SIZE = 1024 * 8; // 64K
041
042    private IOUtils() {
043    }
044
045    private static byte[] createBuffer(int preferredSize) {
046        if (preferredSize < 1) {
047            preferredSize = BUFFER_SIZE;
048        }
049        if (preferredSize > MAX_BUFFER_SIZE) {
050            preferredSize = MAX_BUFFER_SIZE;
051        } else if (preferredSize < MIN_BUFFER_SIZE) {
052            preferredSize = MIN_BUFFER_SIZE;
053        }
054        return new byte[preferredSize];
055    }
056
057    public static void copy(InputStream in, OutputStream out) throws IOException {
058        byte[] buffer = createBuffer(in.available());
059        int read;
060        while ((read = in.read(buffer)) != -1) {
061            out.write(buffer, 0, read);
062        }
063    }
064
065    public static File copyToTempFile(InputStream in) throws IOException {
066        File file = File.createTempFile("nxautomation-", ".tmp", new File(System.getProperty("java.io.tmpdir")));
067        file.deleteOnExit();
068        copyToFile(in, file, true);
069        return file;
070    }
071
072    public static File copyToTempFile(InputStream in, boolean closeIn) throws IOException {
073        File file = File.createTempFile("nxautomation-", ".tmp", new File(System.getProperty("java.io.tmpdir")));
074        file.deleteOnExit();
075        copyToFile(in, file, closeIn);
076        return file;
077    }
078
079    public static void copyToFile(InputStream in, File file) throws IOException {
080        copyToFile(in, file, true);
081    }
082
083    public static void copyToFile(InputStream in, File file, boolean closeIn) throws IOException {
084        FileOutputStream out = new FileOutputStream(file);
085        try {
086            copy(in, out);
087        } finally {
088            out.close();
089            if (closeIn) {
090                in.close();
091            }
092        }
093    }
094
095    public static void writeToFile(String content, File file) throws IOException {
096        FileOutputStream out = new FileOutputStream(file);
097        try {
098            write(content, out);
099        } finally {
100            out.close();
101        }
102    }
103
104    public static void write(String content, OutputStream out) throws IOException {
105        out.write(content.getBytes());
106    }
107
108    public static String read(InputStream in) throws IOException {
109        InputStreamReader reader = new InputStreamReader(in, "UTF-8");
110        return read(reader);
111    }
112
113    public static String read(Reader in) throws IOException {
114        StringBuilder sb = new StringBuilder();
115        try {
116            int read;
117            char[] buffer = new char[64 * 1024];
118            while ((read = in.read(buffer)) != -1) {
119                sb.append(new String(buffer, 0, read));
120            }
121        } finally {
122            in.close();
123        }
124        return sb.toString();
125    }
126
127}