-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cs
More file actions
88 lines (60 loc) · 2 KB
/
FileReader.cs
File metadata and controls
88 lines (60 loc) · 2 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace VS2010Commons
{
public class FileReader
{
public FileReader()
{
}
//public byte[] GetImg(String path)
//{
// try
// {
// ReadableByteChannel channel = new FileInputStream(path).getChannel();
// ByteBuffer buf = ByteBuffer.allocateDirect(12000);
// byte[] result = new byte[12000];
// int numRead = 0;
// while (numRead >= 0)
// {
// buf.rewind();
// numRead = channel.read(buf);
// buf.rewind();
// for (int i = 0; i < numRead; i++)
// {
// byte b = buf.get();
// result[i] = b;
// }
// }
// return result;
// }
// catch (Exception e)
// {
// return null;
// }
//}
public static byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}
}
}