Debian Codegraph @

src/tools/org/h2/dev/ftp/server/FtpData.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.dev.ftp.server;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.net.InetAddress;
12 import java.net.ServerSocket;
13 import java.net.Socket;
14 import org.h2.store.fs.FileUtils;
15 import org.h2.util.IOUtils;
16
17 /**
18 * The implementation of the data channel of the FTP server.
19 */
20 public class FtpData extends Thread {
21
22 private final FtpServer server;
23 private final InetAddress address;
24 private ServerSocket serverSocket;
25 private volatile Socket socket;
26 private final boolean active;
27 private final int port;
28
29 FtpData(FtpServer server, InetAddress address, ServerSocket serverSocket) {
30 this.server = server;
31 this.address = address;
32 this.serverSocket = serverSocket;
33 this.port = 0;
34 this.active = false;
35 }
36
37 FtpData(FtpServer server, InetAddress address, int port) {
38 this.server = server;
39 this.address = address;
40 this.port = port;
41 this.active = true;
42 }
43
44 @Override
45 public void run() {
46 try {
47 synchronized (this) {
48 Socket s = serverSocket.accept();
49 if (s.getInetAddress().equals(address)) {
50 server.trace("Data connected:" + s.getInetAddress() + " expected:" + address);
51 socket = s;
52 notifyAll();
53 } else {
54 server.trace("Data REJECTED:" + s.getInetAddress() + " expected:" + address);
55 close();
56 }
57 }
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 }
62
63 private void connect() throws IOException {
64 if (active) {
65 socket = new Socket(address, port);
66 } else {
67 waitUntilConnected();
68 }
69 }
70
71 private void waitUntilConnected() {
72 while (serverSocket != null && socket == null) {
73 try {
74 wait();
75 } catch (InterruptedException e) {
76 // ignore
77 }
78 }
79 server.trace("connected");
80 }
81
82 /**
83 * Close the socket.
84 */
85 void close() {
86 serverSocket = null;
87 socket = null;
88 }
89
90 /**
91 * Read a file from a client.
92 *
93 * @param fileName the target file name
94 */
95 synchronized void receive(String fileName) throws IOException {
96 connect();
97 try {
98 InputStream in = socket.getInputStream();
99 OutputStream out = FileUtils.newOutputStream(fileName, false);
100 IOUtils.copy(in, out);
101 out.close();
102 } finally {
103 socket.close();
104 }
105 server.trace("closed");
106 }
107
108 /**
109 * Send a file to the client. This method waits until the client has
110 * connected.
111 *
112 * @param fileName the source file name
113 * @param skip the number of bytes to skip
114 */
115 synchronized void send(String fileName, long skip) throws IOException {
116 connect();
117 try {
118 OutputStream out = socket.getOutputStream();
119 InputStream in = FileUtils.newInputStream(fileName);
120 IOUtils.skipFully(in, skip);
121 IOUtils.copy(in, out);
122 in.close();
123 } finally {
124 socket.close();
125 }
126 server.trace("closed");
127 }
128
129 /**
130 * Wait until the client has connected, and then send the data to him.
131 *
132 * @param data the data to send
133 */
134 synchronized void send(byte[] data) throws IOException {
135 connect();
136 try {
137 OutputStream out = socket.getOutputStream();
138 out.write(data);
139 } finally {
140 socket.close();
141 }
142 server.trace("closed");
143 }
144
145 }