-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteBlob.java
More file actions
219 lines (201 loc) · 7.71 KB
/
Copy pathSQLiteBlob.java
File metadata and controls
219 lines (201 loc) · 7.71 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2010 ALM Works Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.almworks.sqlite4java;
/**
* SQLiteBlob encapsulates <strong><code>sqlite3_blob*</code></strong> handle, which represents an open BLOB
* (binary large object), stored in a single cell of a table.
* <p>
* SQLiteBlob is created by {@link SQLiteConnection#blob} method. After application is done using the instance
* of SQLiteBlob, it should be disposed with {@link #dispose} method.
* <p>
* You can read or write portions of the stored blob using {@link #read} and {@link #write} methods. Note that
* you cannot change the size of the blob using this interface.
* <p>
* Methods of this class are not thread-safe and confined to the thread that opened the SQLite connection.
*
* @author Igor Sereda
* @see SQLiteConnection#blob
* @see <a href="http://www.sqlite.org/c3ref/blob_open.html">sqlite3_blob_open</a>
*/
public final class SQLiteBlob {
/**
* Debug name
*/
private final String myName;
/**
* Whether blob was opened for writing
*/
private final boolean myWriteAccess;
/**
* Controller, not null
*/
private SQLiteController myController;
/**
* Handle, set to null when disposed
*/
private SWIGTYPE_p_sqlite3_blob myHandle;
/**
* Cached length
*/
private int myLength = -1;
SQLiteBlob(SQLiteController controller, SWIGTYPE_p_sqlite3_blob handle, String dbname, String table, String column,
long rowid, boolean writeAccess)
{
assert controller != null;
assert handle != null;
myController = controller;
myHandle = handle;
myWriteAccess = writeAccess;
myName = dbname + "." + table + "." + column + ":" + rowid;
}
/**
* Disposes this blob and frees allocated resources.
* <p>
* After blob is disposed, it is no longer usable and holds no references to connection
* or sqlite db.
*/
public void dispose() {
if (myHandle == null)
return;
try {
myController.validate();
} catch (SQLiteException e) {
Internal.recoverableError(this, "invalid dispose: " + e, true);
return;
}
Internal.logFine(this, "disposing");
myController.dispose(this);
// clear may be called from dispose() too
clear();
}
/**
* Checks if this instance has been disposed
*
* @return true if the blob is disposed and cannot be used
*/
public boolean isDisposed() {
return myHandle == null;
}
/**
* Returns the size of the open blob. The size cannot be changed via this interface.
*
* @return size of the blobs in bytes
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
*/
public int getSize() throws SQLiteException {
myController.validate();
if (myLength < 0) {
myLength = _SQLiteSwigged.sqlite3_blob_bytes(handle());
}
return myLength;
}
/**
* Read bytes from the blob into a buffer.
* <p>
* <code>blobOffset</code> and <code>length</code> should define a sub-range within blob's content. If attempt is
* made to read blob beyond its size, an exception is thrown and no data is read.
*
* @param blobOffset the position in the blob where to start reading
* @param buffer target buffer
* @param offset starting offset in the buffer
* @param length number of bytes to read
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/blob_read.html">sqlite3_blob_read</a>
*/
public void read(int blobOffset, byte[] buffer, int offset, int length) throws SQLiteException {
if (buffer == null)
throw new NullPointerException();
if (offset < 0 || offset + length > buffer.length)
throw new ArrayIndexOutOfBoundsException(buffer.length + " " + offset + " " + length);
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "read[" + blobOffset + "," + length + "]");
int rc = _SQLiteManual.sqlite3_blob_read(handle(), blobOffset, buffer, offset, length);
myController.throwResult(rc, "read", this);
}
/**
* Writes bytes into the blob. Bytes are taken from the specified range in the input byte buffer.
* <p>
* Note that you cannot write beyond the current blob's size. The size of the blob
* cannot be changed via incremental I/O API. To change the size, you need to use {@link SQLiteStatement#bindZeroBlob}
* method.
* <p>
* Bytes are written within the current transaction.
* <p>
* If blob was not open for writing, an error is thrown.
*
* @param blobOffset the position in the blob where to start writing
* @param buffer source bytes buffer
* @param offset starting offset in the buffer
* @param length number of bytes to write
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/blob_write.html">sqlite3_blob_write</a>
*/
public void write(int blobOffset, byte[] buffer, int offset, int length) throws SQLiteException {
if (buffer == null)
throw new NullPointerException();
if (offset < 0 || offset + length > buffer.length)
throw new ArrayIndexOutOfBoundsException(buffer.length + " " + offset + " " + length);
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "write[" + blobOffset + "," + length + "]");
int rc = _SQLiteManual.sqlite3_blob_write(handle(), blobOffset, buffer, offset, length);
myController.throwResult(rc, "write", this);
}
/**
* Returns true if this blob instance was opened for writing.
*
* @return true if {@link #write} is allowed
*/
public boolean isWriteAllowed() {
return myWriteAccess;
}
/**
* Repositions BLOB to another row in the table. It should be quickier that closing the blob and opening another one.
*
* @param rowid row id to move to - it must exist and contain data
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/blob_reopen.html">sqlite3_blob_reopen</a>
*/
public void reopen(long rowid) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "reopen[" + rowid + "]");
int rc = _SQLiteSwigged.sqlite3_blob_reopen(handle(), rowid);
myController.throwResult(rc, "reopen", this);
}
private SWIGTYPE_p_sqlite3_blob handle() throws SQLiteException {
SWIGTYPE_p_sqlite3_blob handle = myHandle;
if (handle == null) {
throw new SQLiteException(SQLiteConstants.WRAPPER_BLOB_DISPOSED, null);
}
return handle;
}
SWIGTYPE_p_sqlite3_blob blobHandle() {
return myHandle;
}
/**
* Clear all data, disposing the blob. May be called by SQLiteConnection on close.
*/
void clear() {
myHandle = null;
myController = SQLiteController.getDisposed(myController);
Internal.logFine(this, "cleared");
}
public String toString() {
return "[" + myName + "]" + myController;
}
}