Java InputStream to OutputStream CopyStream(InputStream sSource, OutputStream sTarget)

Here you can find the source of CopyStream(InputStream sSource, OutputStream sTarget)

Description

Copy Stream

License

Open Source License

Declaration

public static void CopyStream(InputStream sSource, OutputStream sTarget) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w w w. ja v  a  2  s  . c  o  m*/
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
    
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

import java.io.*;

public class Main {
    public static void CopyStream(InputStream sSource, OutputStream sTarget) throws IOException {
        assert (sSource != null && (sTarget != null));
        if (sSource == null)
            throw new IllegalArgumentException("sSource");
        if (sTarget == null)
            throw new IllegalArgumentException("sTarget");

        final int nBufSize = 4096;
        byte[] pbBuf = new byte[nBufSize];

        int read;
        while ((read = sSource.read(pbBuf, 0, nBufSize)) != -1) {
            sTarget.write(pbBuf, 0, read);
        }

        // Do not close any of the streams
    }

    public static byte[] Read(InputStream s, int nCount) throws IOException {
        if (s == null)
            throw new IllegalArgumentException("s");
        if (nCount < 0)
            throw new ArrayIndexOutOfBoundsException("nCount");

        byte[] pb = new byte[nCount];
        int iOffset = 0;
        while (nCount > 0) {
            int iRead = s.read(pb, iOffset, nCount);
            if (iRead == -1)
                break;

            iOffset += iRead;
            nCount -= iRead;
        }

        if (iOffset != pb.length) {
            byte[] pbPart = new byte[iOffset];
            System.arraycopy(pb, 0, pbPart, 0, iOffset);
            return pbPart;
        }

        return pb;
    }

    public static void Write(OutputStream s, byte[] pbData) throws IOException {
        if (s == null) {
            assert false;
            return;
        }
        if (pbData == null) {
            assert false;
            return;
        }

        s.write(pbData, 0, pbData.length);
    }
}

Related

  1. copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
  2. copyStream(InputStream src, OutputStream dest)
  3. copyStream(InputStream src, OutputStream dest)
  4. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
  5. copyStream(InputStream src, OutputStream osstream)
  6. copyStream(int bufferSize, InputStream in, OutputStream out, boolean canStop)
  7. copyStream(java.io.InputStream in, java.io.OutputStream out)
  8. copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
  9. copyStream(java.io.InputStream src, java.io.OutputStream dest)