Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

In this page you can find the example usage for java.io OutputStream write.

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this output stream.

Usage

From source file:Main.java

public static void copyFolder(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        //if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();/*  www  .ja  v  a  2 s  . c om*/
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
            //construct the src and dest file structure
            File srcFile = new File(src, file);
            File destFile = new File(dest, file);
            //recursive copy
            copyFolder(srcFile, destFile);
        }

    } else {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
    }
}

From source file:com.amazonaws.encryptionsdk.internal.TestIOUtils.java

public static void copyInStreamToOutStream(final InputStream inStream, final OutputStream outStream,
        final int readLen) throws IOException {
    final byte[] readBuffer = new byte[readLen];
    int actualRead = 0;
    while (actualRead >= 0) {
        outStream.write(readBuffer, 0, actualRead);
        actualRead = inStream.read(readBuffer);
    }//ww  w. j  a v a 2  s  .com
    inStream.close();
    outStream.close();
}

From source file:Main.java

static File makeTempJar(File moduleFile) throws IOException {
    String prefix = moduleFile.getName();
    if (prefix.endsWith(".jar") || prefix.endsWith(".JAR")) { // NOI18N
        prefix = prefix.substring(0, prefix.length() - 4);
    }//from  ww  w. j a  va 2s.  c om
    if (prefix.length() < 3)
        prefix += '.';
    if (prefix.length() < 3)
        prefix += '.';
    if (prefix.length() < 3)
        prefix += '.';
    String suffix = "-test.jar"; // NOI18N
    File physicalModuleFile = File.createTempFile(prefix, suffix);
    physicalModuleFile.deleteOnExit();
    InputStream is = new FileInputStream(moduleFile);
    try {
        OutputStream os = new FileOutputStream(physicalModuleFile);
        try {
            byte[] buf = new byte[4096];
            int i;
            while ((i = is.read(buf)) != -1) {
                os.write(buf, 0, i);
            }
        } finally {
            os.close();
        }
    } finally {
        is.close();
    }
    return physicalModuleFile;
}

From source file:StreamUtils.java

/**
 * Copies all bytes from an InputStream to an OutputStream. The buffer size
 * is set to 8000 bytes./*from ww w .j ava 2  s  .c o  m*/
 * 
 * @param _isInput
 *            InputStream from wihch the bytes are to copied.
 * @param _osOutput
 *            OutputStream in which the bytes are copied.
 * @return Number of bytes which are copied.
 * @throws IOException
 *             If the Streams are unavailable.
 */
public static long streamCopy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[BUFFERSIZE];
    int read;
    long copied = 0;

    while ((read = in.read(buffer)) > 0) {
        out.write(buffer, 0, read);
        copied += read;
    }

    return copied;
}

From source file:cz.ceskaexpedice.k5.k5jaas.basic.K5LoginModule.java

public static void copyStreams(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[8192];
    int read = -1;
    while ((read = is.read(buffer)) > 0) {
        os.write(buffer, 0, read);
    }//from  ww w  .  j  av  a2 s.  c  om
}

From source file:com.aurel.track.exchange.UploadHelper.java

/**
 * Upload the file in a person and type specific directory
 * @return/*from  w  w w  .j  a va 2 s .  c  om*/
 */
public static String upload(File uploadFile, String uploadFileFileName, String targetPath, Locale locale,
        String successResult) {
    InputStream inputStream;
    try {
        inputStream = new FileInputStream(uploadFile);
    } catch (FileNotFoundException e) {
        LOGGER.error("Getting the input stream for the  uploaded file failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
        JSONUtility.encodeJSON(ServletActionContext.getResponse(), JSONUtility.encodeJSONFailure(LocalizeUtil
                .getLocalizedTextFromApplicationResources("admin.actions.importTp.err.failed", locale)));
        return null;
    }
    UploadHelper.ensureDir(targetPath);
    File targetFile = new File(targetPath, uploadFileFileName);
    if (targetFile.exists()) {
        //if the file exists (as a result of a previous import) then delete it
        targetFile.delete();
    }
    try {
        OutputStream outputStream = new FileOutputStream(targetFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        inputStream.close();
        outputStream.close();
    } catch (Exception e) {
        LOGGER.error("Saving the file " + uploadFileFileName + " to the temporary directory " + targetPath
                + " failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
        JSONUtility.encodeJSON(ServletActionContext.getResponse(), JSONUtility.encodeJSONFailure(LocalizeUtil
                .getLocalizedTextFromApplicationResources("admin.actions.importTp.err.failed", locale)));
        return null;
    }
    return successResult;
}

From source file:javarestart.Utils.java

static void copy(InputStream in, OutputStream out, int length) throws IOException {
    int nRead;// w  ww  . j ava2  s  . com
    byte[] data = new byte[16384];

    while ((nRead = in.read(data, 0, Math.min(length, data.length))) != -1) {
        out.write(data, 0, nRead);
        length -= nRead;
        if (length == 0) {
            return;
        }
    }
}

From source file:com.ns.cm.ProvisionServlet.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] buf = new byte[4096];
    try {//from   ww w  .  ja va  2  s . co m
        int n = 0;
        while (is != null && (n = is.read(buf)) > 0)
            os.write(buf, 0, n);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
        try {
            if (os != null)
                os.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static void copyRawFile(Context ctx, String rawFileName, String to) {
    String[] names = rawFileName.split("\\.");
    String toFile = to + "/" + names[0] + "." + names[1];
    File file = new File(toFile);
    if (file.exists()) {
        return;// w w w  . j  a  v  a  2s .  co  m
    }
    try {
        InputStream is = getStream(ctx, "raw://" + names[0]);
        OutputStream os = new FileOutputStream(toFile);
        int byteCount = 0;
        byte[] bytes = new byte[1024];

        while ((byteCount = is.read(bytes)) != -1) {
            os.write(bytes, 0, byteCount);
        }
        os.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.isif.util_plus.http.client.multipart.HttpMultipart.java

private static void writeBytes(final ByteArrayBuffer b, final OutputStream out) throws IOException {
    out.write(b.buffer(), 0, b.length());
    out.flush();/*  www .j  a  v a 2  s .  com*/
}