~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/org/blobstreaming/jdbc/Util.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * Paul McCullagh
 
20
 *
 
21
 * 2007-07-30
 
22
 *
 
23
 * H&G2JCtL
 
24
 *
 
25
 * JDBC Utilities
 
26
 *
 
27
 */
 
28
 
 
29
package org.blobstreaming.jdbc;
 
30
 
 
31
import org.blobstreaming.BlobStream;
 
32
import org.blobstreaming.Connection;
 
33
import org.blobstreaming.www.HTTPException;
 
34
 
 
35
import java.io.IOException;
 
36
import java.io.ByteArrayInputStream;
 
37
import java.io.InputStream;
 
38
 
 
39
import java.sql.SQLException;
 
40
 
 
41
import com.mysql.jdbc.SQLError;
 
42
import com.mysql.jdbc.Messages;
 
43
 
 
44
public class Util {
 
45
        public static boolean isBlobURL(byte blob_url[])
 
46
        {
 
47
                int             i, len;
 
48
 
 
49
                /* ~*test/~1-150-2b5e0a7-0[.ext] */
 
50
                /* ~*test/_1-150-2b5e0a7-0[.ext] */
 
51
                if (blob_url != null) {
 
52
                        len = blob_url.length;
 
53
 
 
54
                        /* Too short: */
 
55
                        if (len <= 10)
 
56
                                return false;
 
57
 
 
58
                        /* Required prefix: */
 
59
                        if (blob_url[0] != '~' || (blob_url[1] != '>' && blob_url[1] != '*'))
 
60
                                return false;
 
61
 
 
62
                        i = len - 1;
 
63
 
 
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] != '.')
 
67
                                        i--;
 
68
                                if (i == 0 || blob_url[i] != '.')
 
69
                                        return false;
 
70
                                if (i == 0 || !(blob_url[i] >= '0' && blob_url[i] <= '9'))
 
71
                                        return false;
 
72
                        }
 
73
 
 
74
                        while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
 
75
                                i--;
 
76
 
 
77
                        if (i == 0 || blob_url[i] != '-')
 
78
                                return false;
 
79
                        i--;
 
80
                        if (!((blob_url[i] >= '0' && blob_url[i] <= '9') || (blob_url[i] >= 'a' && blob_url[i] <= 'f')))
 
81
                                return false;
 
82
 
 
83
                        while (i > 0 && ((blob_url[i] >= '0' && blob_url[i] <= '9') || (blob_url[i] >= 'a' && blob_url[i] <= 'f')))
 
84
                                i--;
 
85
 
 
86
                        if (i == 0 || blob_url[i] != '-')
 
87
                                return false;
 
88
                        i--;
 
89
                        if (!(blob_url[i] >= '0' && blob_url[i] <= '9'))
 
90
                                return false;
 
91
                                
 
92
                        while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
 
93
                                i--;
 
94
 
 
95
                        if (i == 0 || blob_url[i] != '-')
 
96
                                return false;
 
97
                        i--;
 
98
                        if (!(blob_url[i] >= '0' && blob_url[i] <= '9'))
 
99
                                return false;
 
100
 
 
101
                        while (i > 0 && blob_url[i] >= '0' && blob_url[i] <= '9')
 
102
                                i--;
 
103
 
 
104
                        if (i == 0 || (blob_url[i] != '^' && blob_url[i] != ':' && blob_url[i] != '_' && blob_url[i] != '~'))
 
105
                                return false;
 
106
                        i--;
 
107
 
 
108
                        if (i == 0 || blob_url[i] != '/')
 
109
                                return false;
 
110
                        i--;
 
111
                        if (i == 0)
 
112
                                return false;
 
113
                        return true;
 
114
                }
 
115
                return false;
 
116
        }
 
117
 
 
118
        public static BlobStream getInternalBlob(com.mysql.jdbc.Connection myconn, byte url[]) throws SQLException
 
119
        {
 
120
                Connection      conn = null;
 
121
                BlobStream      blob = null;
 
122
                String          address;
 
123
 
 
124
                address = myconn.getHost() + ":" + myconn.getServerVariable("mybs_port");
 
125
                try {
 
126
                        conn = Pool.getConnection(address);
 
127
                        blob = conn.download(new String(url));
 
128
                }
 
129
                catch (HTTPException e) {
 
130
                        conn.close();
 
131
                        blob = new BlobStream(null);
 
132
                }
 
133
                catch (Exception e) {
 
134
                        conn.close();
 
135
                        throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
 
136
                                        + e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);
 
137
                }
 
138
                return blob;
 
139
        }
 
140
 
 
141
        public static byte[] getInternalBytes(com.mysql.jdbc.Connection conn, byte url[], long start, int length) throws SQLException
 
142
        {
 
143
                BlobStream      blob = getInternalBlob(conn, url);
 
144
                byte            buffer[];
 
145
                
 
146
                try {
 
147
                        buffer = blob.getData(start, length);
 
148
                }
 
149
                catch (IOException e) {
 
150
                        throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
 
151
                                        + e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);
 
152
                }
 
153
                return buffer;
 
154
        }
 
155
 
 
156
        public static byte[] getBytes(com.mysql.jdbc.Connection conn, byte url[], long pos, int length) throws SQLException
 
157
        {
 
158
                if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
 
159
                        return getInternalBytes(conn, url, pos, length);
 
160
                return url;
 
161
        }
 
162
 
 
163
        public static InputStream getStream(com.mysql.jdbc.Connection conn, byte url[]) throws SQLException
 
164
        {
 
165
                if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
 
166
                        return (InputStream) getInternalBlob(conn, url);
 
167
                return new ByteArrayInputStream(url);
 
168
        }
 
169
 
 
170
        public static java.sql.Blob getBlob(com.mysql.jdbc.Connection conn, byte url[]) throws SQLException
 
171
        {
 
172
                if (url.length >= 10 && url[0] == '~' && isBlobURL(url))
 
173
                        return new Blob(conn, url);
 
174
                return null;
 
175
        }
 
176
 
 
177
        public static int putStream(com.mysql.jdbc.Connection myconn, byte url[], InputStream in, long length) throws SQLException
 
178
        {
 
179
                Connection      conn = null;
 
180
                String          address;
 
181
                int                     len;
 
182
 
 
183
                address = myconn.getHost() + ":" + myconn.getServerVariable("mybs_port");
 
184
                try {
 
185
                        conn = Pool.getConnection(address);
 
186
                        len = conn.upload(myconn.getDatabase(), "application/octet-stream", in, length, url);
 
187
                }
 
188
                catch (HTTPException e) {
 
189
                        conn.close();
 
190
                        len = 0;
 
191
                }
 
192
                catch (Exception e) {
 
193
                        conn.close();
 
194
                        throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$
 
195
                                        + e.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR);
 
196
                }
 
197
                return len;
 
198
        }
 
199
}