DefaultProxyTransfer.java 4.67 KB
Newer Older
1
/*
2
 * Copyright (C) 1999-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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.
15
 */
16
package org.jivesoftware.openfire.filetransfer.proxy;
17 18

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
import java.util.concurrent.Future;

import org.jivesoftware.util.cache.CacheSizes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
26 27 28 29 30 31 32

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

33 34
	private static final Logger Log = LoggerFactory.getLogger(DefaultProxyTransfer.class);

35 36
    private String initiator;

Alex Wenckus's avatar
Alex Wenckus committed
37
    private InputStream inputStream;
38

Alex Wenckus's avatar
Alex Wenckus committed
39
    private OutputStream outputStream;
40 41 42 43 44 45 46 47 48 49 50

    private String target;

    private String transferDigest;

    private String streamID;

    private Future<?> future;

    private long amountWritten;

51 52
    private static final int BUFFER_SIZE = 8000;

53 54 55
    public DefaultProxyTransfer() { }


56
    @Override
57 58 59 60
    public String getInitiator() {
        return initiator;
    }

61
    @Override
62 63 64 65
    public void setInitiator(String initiator) {
        this.initiator = initiator;
    }

66
    @Override
Alex Wenckus's avatar
Alex Wenckus committed
67
    public InputStream getInputStream() {
68
        return inputStream;
69 70
    }

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

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

81
    @Override
Alex Wenckus's avatar
Alex Wenckus committed
82 83
    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
84 85
    }

86
    @Override
87 88 89 90
    public String getTarget() {
        return target;
    }

91
    @Override
92 93 94 95
    public void setTarget(String target) {
        this.target = target;
    }

96
    @Override
97 98 99 100
    public String getTransferDigest() {
        return transferDigest;
    }

101
    @Override
102 103 104 105
    public void setTransferDigest(String transferDigest) {
        this.transferDigest = transferDigest;
    }

106
    @Override
107 108 109 110
    public String getSessionID() {
        return streamID;
    }

111
    @Override
112 113 114 115 116
    public void setSessionID(String streamID) {
        this.streamID = streamID;
    }


117
    @Override
118
    public boolean isActivatable() {
119
        return ((inputStream != null) && (outputStream != null));
120 121
    }

122
    @Override
123 124 125 126 127 128 129
    public synchronized void setTransferFuture(Future<?> future) {
        if(this.future != null) {
            throw new IllegalStateException("Transfer is already in progress, or has completed.");
        }
        this.future = future;
    }

130
    @Override
131
    public long getAmountTransferred() {
132 133 134
        return amountWritten;
    }

135
    @Override
136
    public void doTransfer() throws IOException {
137
        if (!isActivatable()) {
138 139 140
            throw new IOException("Transfer missing party");
        }

141 142
        try (InputStream in = getInputStream()) {
            try (OutputStream out = new ProxyOutputStream(getOutputStream())) {
143

144 145 146
                final byte[] b = new byte[BUFFER_SIZE];
                int count = 0;
                amountWritten = 0;
147

148 149 150
                do {
                    // write to the output stream
                    out.write(b, 0, count);
151

152
                    amountWritten += count;
153

154 155 156
                    // read more bytes from the input stream
                    count = in.read(b);
                } while (count >= 0);
157 158
            }
        }
159 160
    }

161
    @Override
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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;
    }

}