1
/* Copyright (c) 2007 SNAP Innovation GmbH
3
* BLOB Streaming for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
package org.blobstreaming.jdbc;
31
import org.blobstreaming.BlobStream;
32
import org.blobstreaming.Connection;
33
import org.blobstreaming.www.HTTPException;
35
import java.io.IOException;
36
import java.io.ByteArrayInputStream;
37
import java.io.InputStream;
39
import java.sql.SQLException;
41
import com.mysql.jdbc.SQLError;
42
import com.mysql.jdbc.Messages;
45
public static boolean isBlobURL(byte blob_url[])
49
/* ~*test/~1-150-2b5e0a7-0[.ext] */
50
/* ~*test/_1-150-2b5e0a7-0[.ext] */
51
if (blob_url != null) {
52
len = blob_url.length;
58
/* Required prefix: */
59
if (blob_url[0] != '~' || (blob_url[1] != '>' && blob_url[1] != '*'))
64
/* Allow for an optional extension: */
65
if (!(blob_url[i] >= '0' && blob_url[i] <= '9')) {
66
while (i > 0 && blob_url[i] != '/' && blob_url[i] != '.')
68
if (i == 0 || blob_url[i] != '.')
70
if (i == 0 || !(blob_url[i] >= '0' && blob_url[i] <= '9'))
74
while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
77
if (i == 0 || blob_url[i] != '-')
80
if (!((blob_url[i] >= '0' && blob_url[i] <= '9') || (blob_url[i] >= 'a' && blob_url[i] <= 'f')))
83
while (i > 0 && ((blob_url[i] >= '0' && blob_url[i] <= '9') || (blob_url[i] >= 'a' && blob_url[i] <= 'f')))
86
if (i == 0 || blob_url[i] != '-')
89
if (!(blob_url[i] >= '0' && blob_url[i] <= '9'))
92
while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
95
if (i == 0 || blob_url[i] != '-')
98
if (!(blob_url[i] >= '0' && blob_url[i] <= '9'))
101
while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
104
if (i == 0 || (blob_url[i] != '^' && blob_url[i] != ':' && blob_url[i] != '_' && blob_url[i] != '~'))
108
if (i == 0 || blob_url[i] != '/')
118
public static BlobStream getInternalBlob(com.mysql.jdbc.Connection myconn, byte url[]) throws SQLException
120
Connection conn = null;
121
BlobStream blob = null;
124
address = myconn.getHost() + ":" + myconn.getServerVariable("mybs_port");
126
conn = Pool.getConnection(address);
127
blob = conn.download(new String(url));
129
catch (HTTPException e) {
131
blob = new BlobStream(null);
133
catch (Exception e) {
135
throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
136
+ e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);
141
public static byte[] getInternalBytes(com.mysql.jdbc.Connection conn, byte url[], long start, int length) throws SQLException
143
BlobStream blob = getInternalBlob(conn, url);
147
buffer = blob.getData(start, length);
149
catch (IOException e) {
150
throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
151
+ e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);
156
public static byte[] getBytes(com.mysql.jdbc.Connection conn, byte url[], long pos, int length) throws SQLException
158
if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
159
return getInternalBytes(conn, url, pos, length);
163
public static InputStream getStream(com.mysql.jdbc.Connection conn, byte url[]) throws SQLException
165
if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
166
return (InputStream) getInternalBlob(conn, url);
167
return new ByteArrayInputStream(url);
170
public static java.sql.Blob getBlob(com.mysql.jdbc.Connection conn, byte url[]) throws SQLException
172
if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
173
return new Blob(conn, url);
177
public static int putStream(com.mysql.jdbc.Connection myconn, byte url[], InputStream in, long length) throws SQLException
179
Connection conn = null;
183
address = myconn.getHost() + ":" + myconn.getServerVariable("mybs_port");
185
conn = Pool.getConnection(address);
186
len = conn.upload(myconn.getDatabase(), "application/octet-stream", in, length, url);
188
catch (HTTPException e) {
192
catch (Exception e) {
194
throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
195
+ e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);