-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileRequestMessage.java
65 lines (53 loc) · 1.9 KB
/
FileRequestMessage.java
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
import java.io.*;
public class FileRequestMessage extends Message
{
/*
Amber Krause
December 6, 2016
CISC 230-02
Doctor Jarvis
This class models a message that requests a file.
On the server side, the get method will send the
file back to the client.
Class variables:
serialVersionUID
a serialVersionUID used for serialization.
file
the File in the shared directory to which the
file should be copied. This File is created
with a relative path.
Constructors:
FileRequestMessage(File file)
creates a new FileRequestMessage object set
to the value of the parameter.
Methods:
get(File directoryOfSharedFiles, OutputStream outputStream)
sends a File to the client who requested it.
Modification History:
December 6, 2016
original version.
*/
private static final long serialVersionUID = 1;
private File file;
public FileRequestMessage(File file)
{
if(file == null) throw new IllegalArgumentException("FileRequestMessage.constructor: parameter is null");
this.file = file;
} //constructor
public void get(File directoryOfSharedFiles, OutputStream outputStream) throws IOException
{
//send the file from the directory of shared files to the client
if(directoryOfSharedFiles == null) throw new IllegalArgumentException("FileRequestMessage.get: File parameter is null");
if(outputStream == null) throw new IllegalArgumentException("FileRequestMessage.get: OutputStream parameter is null");
BinaryFileCopier fileCopier;
File fileToBeCopied;
FileInputStream inputStream;
fileCopier = new BinaryFileCopier();
fileToBeCopied = new File(directoryOfSharedFiles, file.toString());
inputStream = new FileInputStream(fileToBeCopied);
fileCopier.copyFile(inputStream, outputStream, 10000);
outputStream.close();
inputStream.close();
System.out.println("Sent the requested file to a client: " + file);
} //get
} //class FileRequestMessage