All pastes #982495 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-12 16:56 UTC
rendered paste body
//This is the code that uploads the file - is most of the run function of a thread.

            Checksum cksum = new CRC32();

            if (VERBOSE) {
                System.out.println("Sending file " + fileToSend.getName() + " to " + remoteAddress);
            }

            try {
                transferSocket = serverSock.accept();
                os = new DataOutputStream(new CheckedOutputStream(transferSocket.getOutputStream(), cksum));
                is = new DataInputStream(new FileInputStream(fileToSend));

                byte[] buffer = new byte[sendBufferSize];
                int i = 0;
                while (running && (i = is.read(buffer)) > 0) {
                    for (int j = 0; j < i; ++j) {
                        os.write(buffer[j]);
                    }
                }
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
                if (transferSocket != null) {
                    transferSocket.close();
                }

                Thread.sleep(200);

            } catch (Exception ex) {
                ex.printStackTrace();
            }


//This is the downloadFile function of the client which recieves and outputs the file.
//returns the cksum as a String

    private static String downloadFile(File outputFile) {
        Checksum cksum = new CRC32();
        if (TCPSocket == null) {
            return null;
        }
        try {
            DataInputStream is = new DataInputStream(new CheckedInputStream(TCPSocket.getInputStream(), cksum));
            FileOutputStream os = new FileOutputStream(outputFile);
            byte[] buffer = new byte[recieveBufferSize];
            int i = 0;
            while ((i = is.read(buffer)) > 0) {
                for (int j = 0; j < i; ++j) {
                    os.write(buffer[j]);
                }
            }
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return ((Long) cksum.getValue()).toString();
    }