DefaultProxyTransfer.java 4.47 KB
Newer Older
1 2
/**
 * $RCSfile$
3 4
 * $Revision: 3762 $
 * $Date: 2006-04-12 18:07:15 -0500 (Mon, 12 Apr 2005) $
5
 *
6
 * Copyright (C) 1999-2008 Jive Software. All rights reserved.
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
11
 */
12
package org.jivesoftware.openfire.filetransfer.proxy;
13

14
import org.jivesoftware.util.cache.CacheSizes;
15
import org.jivesoftware.util.Log;
16 17 18

import java.util.concurrent.Future;
import java.io.IOException;
Alex Wenckus's avatar
Alex Wenckus committed
19 20
import java.io.InputStream;
import java.io.OutputStream;
21 22 23 24 25 26 27 28 29

/**
 * Tracks the different connections related to a file transfer. There are two connections, the
 * initiator and the target and when both connections are completed the transfer can begin.
 */
public class DefaultProxyTransfer implements ProxyTransfer {

    private String initiator;

Alex Wenckus's avatar
Alex Wenckus committed
30
    private InputStream inputStream;
31

Alex Wenckus's avatar
Alex Wenckus committed
32
    private OutputStream outputStream;
33 34 35 36 37 38 39 40 41 42 43

    private String target;

    private String transferDigest;

    private String streamID;

    private Future<?> future;

    private long amountWritten;

44 45
    private static final int BUFFER_SIZE = 8000;

46 47 48 49 50 51 52 53 54 55 56
    public DefaultProxyTransfer() { }


    public String getInitiator() {
        return initiator;
    }

    public void setInitiator(String initiator) {
        this.initiator = initiator;
    }

Alex Wenckus's avatar
Alex Wenckus committed
57
    public InputStream getInputStream() {
58
        return inputStream;
59 60
    }

Alex Wenckus's avatar
Alex Wenckus committed
61 62
    public void setInputStream(InputStream initiatorInputStream) {
        this.inputStream = initiatorInputStream;
63 64
    }

Alex Wenckus's avatar
Alex Wenckus committed
65
    public OutputStream getOutputStream() {
66
        return outputStream;
67 68
    }

Alex Wenckus's avatar
Alex Wenckus committed
69 70
    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getTransferDigest() {
        return transferDigest;
    }

    public void setTransferDigest(String transferDigest) {
        this.transferDigest = transferDigest;
    }

    public String getSessionID() {
        return streamID;
    }

    public void setSessionID(String streamID) {
        this.streamID = streamID;
    }


    public boolean isActivatable() {
99
        return ((inputStream != null) && (outputStream != null));
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    }

    public synchronized void setTransferFuture(Future<?> future) {
        if(this.future != null) {
            throw new IllegalStateException("Transfer is already in progress, or has completed.");
        }
        this.future = future;
    }

    public long getAmountTransfered() {
        return amountWritten;
    }

    public void doTransfer() throws IOException {
114
        if (!isActivatable()) {
115 116
            throw new IOException("Transfer missing party");
        }
117 118
        InputStream in = null;
        OutputStream out = null;
119

120 121 122
        try {
            in = getInputStream();
            out = new ProxyOutputStream(getOutputStream());
123

124 125 126
            final byte[] b = new byte[BUFFER_SIZE];
            int count = 0;
            amountWritten = 0;
127

128 129 130
            do {
                // write to the output stream
                out.write(b, 0, count);
131

132
                amountWritten += count;
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                // read more bytes from the input stream
                count = in.read(b);
            } while (count >= 0);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (Exception e) {
                    Log.error(e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                }
                catch (Exception e) {
                    Log.error(e);
                }
            }
        }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    }

    public int getCachedSize() {
        // Approximate the size of the object in bytes by calculating the size
        // of each field.
        int size = 0;
        size += CacheSizes.sizeOfObject();              // overhead of object
        size += CacheSizes.sizeOfString(initiator);
        size += CacheSizes.sizeOfString(target);
        size += CacheSizes.sizeOfString(transferDigest);
        size += CacheSizes.sizeOfString(streamID);
        size += CacheSizes.sizeOfLong();  // Amount written
        size += CacheSizes.sizeOfObject(); // Initiatior Socket
        size += CacheSizes.sizeOfObject(); // Target socket
        size += CacheSizes.sizeOfObject(); // Future
        return size;
    }

}