DefaultProxyTransfer.java 4.98 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 9 10 11 12 13 14 15 16 17 18
 * 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.
19
 */
20
package org.jivesoftware.openfire.filetransfer.proxy;
21 22

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

import org.jivesoftware.util.cache.CacheSizes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
30 31 32 33 34 35 36

/**
 * 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 {

37 38
	private static final Logger Log = LoggerFactory.getLogger(DefaultProxyTransfer.class);

39 40
    private String initiator;

Alex Wenckus's avatar
Alex Wenckus committed
41
    private InputStream inputStream;
42

Alex Wenckus's avatar
Alex Wenckus committed
43
    private OutputStream outputStream;
44 45 46 47 48 49 50 51 52 53 54

    private String target;

    private String transferDigest;

    private String streamID;

    private Future<?> future;

    private long amountWritten;

55 56
    private static final int BUFFER_SIZE = 8000;

57 58 59 60 61 62 63 64 65 66 67
    public DefaultProxyTransfer() { }


    public String getInitiator() {
        return initiator;
    }

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

Alex Wenckus's avatar
Alex Wenckus committed
68
    public InputStream getInputStream() {
69
        return inputStream;
70 71
    }

Alex Wenckus's avatar
Alex Wenckus committed
72 73
    public void setInputStream(InputStream initiatorInputStream) {
        this.inputStream = initiatorInputStream;
74 75
    }

Alex Wenckus's avatar
Alex Wenckus committed
76
    public OutputStream getOutputStream() {
77
        return outputStream;
78 79
    }

Alex Wenckus's avatar
Alex Wenckus committed
80 81
    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }

    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() {
110
        return ((inputStream != null) && (outputStream != null));
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    }

    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 {
125
        if (!isActivatable()) {
126 127
            throw new IOException("Transfer missing party");
        }
128 129
        InputStream in = null;
        OutputStream out = null;
130

131 132 133
        try {
            in = getInputStream();
            out = new ProxyOutputStream(getOutputStream());
134

135 136 137
            final byte[] b = new byte[BUFFER_SIZE];
            int count = 0;
            amountWritten = 0;
138

139 140 141
            do {
                // write to the output stream
                out.write(b, 0, count);
142

143
                amountWritten += count;
144

145 146 147 148 149 150 151 152 153 154
                // read more bytes from the input stream
                count = in.read(b);
            } while (count >= 0);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (Exception e) {
155
                    Log.error(e.getMessage(), e);
156 157 158 159 160 161 162
                }
            }
            if (out != null) {
                try {
                    out.close();
                }
                catch (Exception e) {
163
                    Log.error(e.getMessage(), e);
164 165 166
                }
            }
        }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }

    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;
    }

}