001/*
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.util;
013
014import java.io.File;
015import java.io.FileOutputStream;
016import java.io.IOException;
017import java.io.InputStream;
018import java.io.InputStreamReader;
019import java.io.OutputStream;
020import java.io.Reader;
021
022/**
023 * File is deleted on JVM exit. You should delete it explicitly earlier if you know it won't be used anymore.
024 *
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class IOUtils {
028
029    private static final int BUFFER_SIZE = 1024 * 64; // 64K
030
031    private static final int MAX_BUFFER_SIZE = 1024 * 1024; // 64K
032
033    private static final int MIN_BUFFER_SIZE = 1024 * 8; // 64K
034
035    private IOUtils() {
036    }
037
038    private static byte[] createBuffer(int preferredSize) {
039        if (preferredSize < 1) {
040            preferredSize = BUFFER_SIZE;
041        }
042        if (preferredSize > MAX_BUFFER_SIZE) {
043            preferredSize = MAX_BUFFER_SIZE;
044        } else if (preferredSize < MIN_BUFFER_SIZE) {
045            preferredSize = MIN_BUFFER_SIZE;
046        }
047        return new byte[preferredSize];
048    }
049
050    public static void copy(InputStream in, OutputStream out) throws IOException {
051        byte[] buffer = createBuffer(in.available());
052        int read;
053        while ((read = in.read(buffer)) != -1) {
054            out.write(buffer, 0, read);
055        }
056    }
057
058    public static File copyToTempFile(InputStream in) throws IOException {
059        File file = File.createTempFile("nxautomation-", ".tmp");
060        file.deleteOnExit();
061        copyToFile(in, file, true);
062        return file;
063    }
064
065    public static File copyToTempFile(InputStream in, boolean closeIn) throws IOException {
066        File file = File.createTempFile("nxautomation-", ".tmp");
067        file.deleteOnExit();
068        copyToFile(in, file, closeIn);
069        return file;
070    }
071
072    public static void copyToFile(InputStream in, File file) throws IOException {
073        copyToFile(in, file, true);
074    }
075
076    public static void copyToFile(InputStream in, File file, boolean closeIn) throws IOException {
077        FileOutputStream out = new FileOutputStream(file);
078        try {
079            copy(in, out);
080        } finally {
081            out.close();
082            if (closeIn) {
083                in.close();
084            }
085        }
086    }
087
088    public static void writeToFile(String content, File file) throws IOException {
089        FileOutputStream out = new FileOutputStream(file);
090        try {
091            write(content, out);
092        } finally {
093            out.close();
094        }
095    }
096
097    public static void write(String content, OutputStream out) throws IOException {
098        out.write(content.getBytes());
099    }
100
101    public static String read(InputStream in) throws IOException {
102        InputStreamReader reader = new InputStreamReader(in, "UTF-8");
103        return read(reader);
104    }
105
106    public static String read(Reader in) throws IOException {
107        StringBuilder sb = new StringBuilder();
108        try {
109            int read;
110            char[] buffer = new char[64 * 1024];
111            while ((read = in.read(buffer)) != -1) {
112                sb.append(new String(buffer, 0, read));
113            }
114        } finally {
115            in.close();
116        }
117        return sb.toString();
118    }
119
120}