1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (C) 1999-2008 Jive Software. All rights reserved.
*
* 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.
*/
package org.jivesoftware.openfire.filetransfer.proxy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Future;
import org.jivesoftware.util.cache.CacheSizes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 static final Logger Log = LoggerFactory.getLogger(DefaultProxyTransfer.class);
private String initiator;
private InputStream inputStream;
private OutputStream outputStream;
private String target;
private String transferDigest;
private String streamID;
private Future<?> future;
private long amountWritten;
private static final int BUFFER_SIZE = 8000;
public DefaultProxyTransfer() { }
@Override
public String getInitiator() {
return initiator;
}
@Override
public void setInitiator(String initiator) {
this.initiator = initiator;
}
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public void setInputStream(InputStream initiatorInputStream) {
this.inputStream = initiatorInputStream;
}
@Override
public OutputStream getOutputStream() {
return outputStream;
}
@Override
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
@Override
public String getTarget() {
return target;
}
@Override
public void setTarget(String target) {
this.target = target;
}
@Override
public String getTransferDigest() {
return transferDigest;
}
@Override
public void setTransferDigest(String transferDigest) {
this.transferDigest = transferDigest;
}
@Override
public String getSessionID() {
return streamID;
}
@Override
public void setSessionID(String streamID) {
this.streamID = streamID;
}
@Override
public boolean isActivatable() {
return ((inputStream != null) && (outputStream != null));
}
@Override
public synchronized void setTransferFuture(Future<?> future) {
if(this.future != null) {
throw new IllegalStateException("Transfer is already in progress, or has completed.");
}
this.future = future;
}
@Override
public long getAmountTransferred() {
return amountWritten;
}
@Override
public void doTransfer() throws IOException {
if (!isActivatable()) {
throw new IOException("Transfer missing party");
}
try (InputStream in = getInputStream()) {
try (OutputStream out = new ProxyOutputStream(getOutputStream())) {
final byte[] b = new byte[BUFFER_SIZE];
int count = 0;
amountWritten = 0;
do {
// write to the output stream
out.write(b, 0, count);
amountWritten += count;
// read more bytes from the input stream
count = in.read(b);
} while (count >= 0);
}
}
}
@Override
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;
}
}