Java InputStream Copy nio copy(InputStream in, File f, long startAt)

Here you can find the source of copy(InputStream in, File f, long startAt)

Description

copy

License

Apache License

Declaration

public static void copy(InputStream in, File f, long startAt) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w  w .j a va 2 s  . c  o m*/
 * Copyright 2011 http://pvoutput.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;
import java.nio.channels.Channel;
import java.nio.channels.Selector;

import java.util.zip.ZipFile;

public class Main {
    public static final int MAX_BYTE_BUFFER = 8192;
    public static final byte[] BYTE_ARRAY_BUFFER = new byte[MAX_BYTE_BUFFER];
    public static final char[] CHAR_ARRAY_BUFFER = new char[MAX_BYTE_BUFFER];

    public static void copy(InputStream in, File f, long startAt) throws IOException {
        copy(in, new FileOutputStream(f), startAt, true, true);
    }

    public static void copy(InputStream in, File f) throws IOException {
        copy(in, f, 0);
    }

    public static boolean copy(InputStream in, OutputStream out, long startAt, boolean closeIn, boolean closeOut) {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;

        boolean success = true;

        try {
            bos = new BufferedOutputStream(out, MAX_BYTE_BUFFER);
            bis = new BufferedInputStream(in, MAX_BYTE_BUFFER);

            if (startAt > 0) {
                bis.skip(startAt);
            }

            int read;

            synchronized (BYTE_ARRAY_BUFFER) {
                while ((read = bis.read(BYTE_ARRAY_BUFFER)) > -1) {
                    bos.write(BYTE_ARRAY_BUFFER, 0, read);
                    bos.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

            success = false;
        } finally {
            if (closeOut) {
                close(bos);
            }

            if (closeIn) {
                close(bis);
            }
        }

        return success;
    }

    public static boolean copy(InputStream in, Writer writer, int startAt, boolean close) {
        return copy(in, writer, startAt, close, close);
    }

    public static boolean copy(InputStream in, Writer writer, int startAt, boolean closeIn, boolean closeOut) {
        BufferedWriter bos = null;
        BufferedReader bis = null;

        boolean success = true;

        try {
            bos = new BufferedWriter(writer, MAX_BYTE_BUFFER);
            bis = new BufferedReader(new InputStreamReader(in), MAX_BYTE_BUFFER);

            if (startAt > 0) {
                bis.skip(startAt);
            }

            int read;

            synchronized (CHAR_ARRAY_BUFFER) {
                while ((read = bis.read(CHAR_ARRAY_BUFFER)) > -1) {
                    bos.write(CHAR_ARRAY_BUFFER, 0, read);
                    bos.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

            success = false;
        } finally {
            if (closeOut) {
                close(bos);
            }

            if (closeIn) {
                close(bis);
            }
        }

        return success;
    }

    public static boolean copy(InputStream in, OutputStream out) {
        return copy(in, out, 0, true, true);
    }

    public static void flush(Writer writer) {
        if (writer != null) {
            try {
                writer.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void flush(OutputStream out) {
        if (out != null) {
            try {
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(InputStream o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                o = null;
            }
        }
    }

    public static void close(Writer o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                System.err.print("Could not close object");
            } finally {
                o = null;
            }
        }
    }

    public static void close(Reader o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                System.err.print("Could not close object");
            } finally {
                o = null;
            }
        }
    }

    public static void close(ZipFile o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                o = null;
            }
        }
    }

    public static void close(Selector o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                System.err.print("Could not close object");
            } finally {
                o = null;
            }
        }
    }

    public static void close(Channel o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                System.err.print("Could not close object");
            } finally {
                o = null;
            }
        }
    }

    public static void close(OutputStream o) {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                o = null;
            }
        }
    }
}

Related

  1. copy(InputStream in, OutputStream out)
  2. copy(InputStream in, OutputStream out)
  3. copy(InputStream in, OutputStream out)
  4. copy(InputStream in, OutputStream out)