-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandle.java
More file actions
64 lines (55 loc) · 1.57 KB
/
Copy pathFileHandle.java
File metadata and controls
64 lines (55 loc) · 1.57 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
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.FileInputStream;
import java.io.*;
import java.util.Arrays;
public class FileHandle {
private FileInputStream fileIn;
private byte[] buffer = new byte[52428800];
private File tempFile;
private int readLength = 52428800;
private boolean isReadComplete = false;
private long readFileSize = 0;
public FileHandle(File temp){
tempFile = temp;
try{
fileIn = new FileInputStream(temp);
}catch(FileNotFoundException e){
System.out.println("File not found"+e.getMessage());
}
}
public byte[] read(){
//check for file size and available bytes to be read
int readL = readLength;
try{
//checks if we are at the end of file read
if (fileIn.available() < readLength) {
readL = fileIn.available();
System.out.println("Remaining: " + readL);
}
//reads a specified size of bytes from the file being transfered
this.fileIn.read(buffer,0,readL);
readFileSize += readL;
//checks if the file read is complete
if (readFileSize >= tempFile.length()) {
isReadComplete = true;
System.out.println("File of size " + readFileSize + " has been read");
fileIn.close();
}
}catch(FileNotFoundException e){
System.out.println("File not found"+e.getMessage());
}catch(IOException e){
System.out.println("IO: "+e.getMessage());
}
//to return the actual buffer with actual size
byte[] tempBuffer = Arrays.copyOf(buffer,readL);
return tempBuffer;
}
public byte[] getBuffer(){
return buffer;
}
public long getReadFileSize(){
return readFileSize;
}
public boolean getIsReadComplete(){
return isReadComplete;
}
}