-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataHandler.java
146 lines (131 loc) · 3.46 KB
/
DataHandler.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
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
import java.io.*;
import java.nio.ByteBuffer;
/**
*
* @author Nicholas Bohm
* @author Dakota Vanwormer
*/
public class DataHandler {
private File directory;
private FileOutputStream fos;
private FileInputStream fis;
private ByteArrayOutputStream baos;
/**
* Constructs the datahandler for the receiver.
*
* @param directory
*/
public DataHandler(String directory) {
this.setDirectory(directory);
try {
this.fos = new FileOutputStream(this.directory + "\\text.txt");
this.baos = new ByteArrayOutputStream();
} catch (FileNotFoundException ex) {
}
}
/**
* creates the datahandler for the sender.
*
* @param directory
* @param fileName
*/
public DataHandler(String directory, String fileName) {
try {
this.setDirectory(directory);
this.fis = new FileInputStream(this.directory + "\\" + fileName);
} catch (FileNotFoundException ex) {
}
}
/**
* creates the data handler for the packet.
*/
public DataHandler() {
}
/**
* gets the file length from the file that is being sent.
*
* @param fileName
* @return
*/
public long getFileLength(String fileName) {
File hold = new File(this.directory + "\\" + fileName);
return hold.length();
}
/**
* converts bytes to a file.
*
* @param data
*/
public void ByteToFile(byte[] data) {
try {
this.baos.write(data);
this.baos.writeTo(fos);
this.baos.reset();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* converts a file into bytes of the given size.
*
* @param fileName
* @param size
* @return
*/
public byte[] FileToByte(String fileName, int size) {
int counter = 0;
byte[][] store = new byte[size][1];
try {
for (int i = 0; i < size; i++) {
if (this.fis.read(store[i], 0, 1) != -1) {
counter++;
}
}
byte[] toReturn = new byte[counter];
for (int i = 0; i < counter; i++) {
toReturn[i] = store[i][0];
}
return toReturn;
} catch (FileNotFoundException ex) {
} catch (IOException io) {
}
return null;
}
/**
* converts bytes to ints.
*
* @param convert
* @return
*/
public int byteToInt(byte[] convert) {
ByteBuffer buffer = ByteBuffer.allocate(convert.length);
for (int i = 0; i != convert.length; i++) {
buffer.put(convert[i]);
}
buffer.flip();
return buffer.getInt();
}
/**
* converts ints to bytes.
*
* @param convert
* @param toAllocate
* @return
*/
public byte[] intToByte(int convert, int toAllocate) {
byte[] dst = new byte[toAllocate];
ByteBuffer buffer = ByteBuffer.allocate(toAllocate);
buffer.putInt(convert);
buffer.flip();
buffer.get(dst);
return dst;
}
/**
* sets the directory of the datahandler.
*
* @param path
*/
public void setDirectory(String path) {
this.directory = new File(path);
}
}