This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
/
FileCursor.java
170 lines (146 loc) · 5.18 KB
/
FileCursor.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.ffxcp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.database.AbstractWindowedCursor;
import android.database.CursorWindow;
public class FileCursor extends AbstractWindowedCursor {
public static final String _ID = "_id";
public static final String CHUNK = "chunk";
public static final String LENGTH = "length";
static final String[] DEFCOLUMNS = new String[] {
_ID,
CHUNK,
LENGTH
};
private String filePath = null;
private String [] theColumns = null;
private static final int BUFSIZE = 4096;
private long lFileSize = 0;
private int nCount = 0;
private File theFile = null;
private byte [] theBuffer = null;
private long lOffset = 0;
private long lLength = -1;
public FileCursor(String[] columnNames, String sFilePath, String [] selectionArgs) {
super();
theColumns = (columnNames == null ? DEFCOLUMNS : columnNames);
filePath = sFilePath;
nCount = -1;
if ((selectionArgs != null) && (selectionArgs.length > 0)) {
lOffset = Long.parseLong(selectionArgs[0]);
lLength = Long.parseLong(selectionArgs[1]);
}
if (filePath.length() > 0) {
theFile = new File(filePath);
if (theFile.exists() && theFile.canRead()) {
lFileSize = theFile.length();
// lLength == -1 return everything between lOffset and eof
// lLength == 0 return file length
// lLength > 0 return lLength bytes
if (lLength == -1) {
lFileSize = lFileSize - lOffset;
} else if (lLength == 0) {
// just return the file length
} else {
lFileSize = ((lLength <= (lFileSize - lOffset)) ? lLength : (lFileSize - lOffset));
}
if (lLength != 0) {
nCount = (int) (lFileSize / BUFSIZE);
if ((lFileSize % BUFSIZE) > 0)
nCount++;
} else {
nCount = 1;
}
mRowIdColumnIndex = 0;
}
}
}
public String getColumnName (int columnIndex) {
return theColumns[columnIndex];
}
@Override
public String[] getColumnNames() {
return theColumns;
}
@Override
public int getCount() {
return nCount;
}
@Override
public boolean onMove(int oldPosition, int newPosition) {
boolean bRet = true;
// get rid of old data
mWindow.clear();
bRet = mWindow.setNumColumns(theColumns.length);
fillWindow(newPosition, mWindow);
return bRet;
}
@Override
public void fillWindow (int pos, CursorWindow window) {
int nNumRows = window.getNumRows();
int nCIndex = 0;
window.setStartPosition(0);
if (pos > -1) {
if (nNumRows == 0) {
window.allocRow();
nNumRows = window.getNumRows();
}
if (nNumRows == 1) {
nCIndex = getColumnIndex(LENGTH);
if (nCIndex > -1) {
window.putLong(lFileSize, 0, nCIndex);
}
nCIndex = getColumnIndex(_ID);
if (nCIndex > -1) {
window.putLong((long)pos, 0, nCIndex);
}
nCIndex = getColumnIndex(CHUNK);
if (nCIndex > -1) {
if (lLength != 0) {
byte[] value = getABlob (pos, 1);
window.putBlob(value, 0, nCIndex);
}
}
}
window.setStartPosition(pos);
}
return;
}
public byte[] getABlob (int row, int column) {
int nRead = 0;
int nOffset = 0;
int nBufSize = 0;
if ((column == 1) && (theFile != null)) {
try {
FileInputStream fin = new FileInputStream(theFile);
nOffset = row * BUFSIZE;
if (row < (nCount - 1)) {
nBufSize = BUFSIZE;
} else {
nBufSize = (int) (lFileSize - nOffset);
}
theBuffer = new byte[nBufSize];
if (theBuffer != null) {
if (fin.skip(nOffset + lOffset) == (nOffset + lOffset)) {
if ((nRead = fin.read(theBuffer, 0, nBufSize)) != -1) {
if (nRead != nBufSize) {
return null;
}
}
}
}
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return theBuffer;
}
}