private FileInfo receiveStream()

in src/java/org/apache/ivy/plugins/repository/ssh/Scp.java [354:439]


    private FileInfo receiveStream(Channel channel, String file, OutputStream targetStream)
            throws IOException, RemoteScpException {
        byte[] buffer = new byte[BUFFER_SIZE];

        OutputStream os = channel.getOutputStream();
        InputStream is = channel.getInputStream();
        try {
            if (channel.isConnected()) {
                channel.start();
            } else {
                channel.connect();
            }
        } catch (JSchException jsche) {
            throw new IOException("Channel connection problems", jsche);
        }
        os.write(0x0);
        os.flush();

        FileInfo fileInfo = new FileInfo();

        while (true) {
            int c = is.read();
            if (c < 0) {
                throw new RemoteScpException("Remote scp terminated unexpectedly.");
            }

            String line = receiveLine(is);

            if (c == 'T') {
                parseTLine(line, fileInfo);
                os.write(0x0);
                os.flush();
                continue;
            }
            if (c == 1 || c == 2) {
                throw new RemoteScpException("Remote SCP error: " + line);
            }

            if (c == 'C') {
                parseCLine(line, fileInfo);
                break;
            }
            throw new RemoteScpException("Remote SCP error: " + ((char) c) + line);
        }
        if (targetStream != null) {

            os.write(0x0);
            os.flush();

            try {
                long remain = fileInfo.getLength();

                while (remain > 0) {
                    int trans;
                    if (remain > buffer.length) {
                        trans = buffer.length;
                    } else {
                        trans = (int) remain;
                    }

                    int thisTimeReceived = is.read(buffer, 0, trans);

                    if (thisTimeReceived < 0) {
                        throw new IOException("Remote scp terminated connection unexpectedly");
                    }

                    targetStream.write(buffer, 0, thisTimeReceived);

                    remain -= thisTimeReceived;
                }

                targetStream.close();
            } catch (IOException e) {
                if (targetStream != null) {
                    targetStream.close();
                }
                throw (e);
            }

            readResponse(is);

            os.write(0x0);
            os.flush();
        }
        return fileInfo;
    }