-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileArchiever.java
More file actions
51 lines (47 loc) · 1.52 KB
/
FileArchiever.java
File metadata and controls
51 lines (47 loc) · 1.52 KB
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
import com.sun.deploy.util.ArrayUtil;
import java.io.*;
import java.lang.reflect.Array;
public class FileArchiever {
public void compressData(FileInputStream istream, FileOutputStream ostream)throws IOException
{
//
try(
BufferedInputStream bfs = new BufferedInputStream(istream);
BufferedOutputStream bws = new BufferedOutputStream(ostream))
{
//init
int currentByte = -1,prevByte;
//if file is empty
if((prevByte = bfs.read()) == -1)
return;
int countOfByte = 1;
//
while((currentByte = bfs.read()) != -1)
{
if(currentByte == prevByte)
countOfByte++;
else//write this
{
while(countOfByte - 9 > 0) {
bws.write(prevByte);
bws.write(9);
countOfByte-=9;
}
bws.write(prevByte);
bws.write(countOfByte);
//update values
countOfByte = 1;
}
prevByte = currentByte;
}
//at the end to write
while(countOfByte - 9 > 0) {
bws.write(prevByte);
bws.write(9);
countOfByte-=9;
}
bws.write(prevByte);
bws.write(countOfByte);
}
}
}