|
42 | 42 | final class BlobConnection extends URLConnection |
43 | 43 | { |
44 | 44 | private static final Logger log = LoggerFactory.getLogger(BlobConnection.class); |
| 45 | + private ByteArrayOutputStream bout; |
45 | 46 |
|
46 | | - public BlobConnection(URL url) |
| 47 | + public BlobConnection(URL url) throws IOException |
47 | 48 | { |
48 | 49 | super(url); |
| 50 | + |
| 51 | + connect(); |
49 | 52 | } |
50 | 53 |
|
51 | 54 | @Override |
52 | 55 | public void connect() throws IOException |
53 | 56 | { |
54 | | - } |
| 57 | + if (bout != null) |
| 58 | + return; |
55 | 59 |
|
56 | | - @Override |
57 | | - public InputStream getInputStream() throws IOException |
58 | | - { |
59 | 60 | try |
60 | 61 | { |
61 | 62 | final DbClassLoader classLoader = (DbClassLoader) Thread.currentThread().getContextClassLoader(); |
62 | 63 | final Connection conn = classLoader.getConnection(); |
63 | | - final PreparedStatement stmt = conn.prepareStatement("select content from sqlj.read_jar(?)"); |
64 | | - ResultSet rs = null; |
65 | 64 |
|
66 | | - try |
| 65 | + try (PreparedStatement stmt = conn.prepareStatement("select content from sqlj.read_jar(?)")) |
67 | 66 | { |
68 | 67 | String urlStr = url.toString().substring(8); // "fbjava:/" |
69 | 68 | stmt.setString(1, urlStr); |
70 | | - final ResultSet finalRs = rs = stmt.executeQuery(); |
71 | 69 |
|
72 | | - if (rs.next()) |
| 70 | + try (ResultSet rs = stmt.executeQuery()) |
73 | 71 | { |
74 | | - return new InputStream() { |
75 | | - private InputStream inner = finalRs.getBinaryStream(1); |
76 | | - |
77 | | - @Override |
78 | | - public int read() throws IOException |
79 | | - { |
80 | | - return inner.read(); |
81 | | - } |
82 | | - |
83 | | - @Override |
84 | | - public int read(byte[] b, int off, int len) throws IOException |
85 | | - { |
86 | | - return inner.read(b, off, len); |
87 | | - } |
88 | | - |
89 | | - @Override |
90 | | - public long skip(long n) throws IOException |
91 | | - { |
92 | | - return inner.skip(n); |
93 | | - } |
94 | | - |
95 | | - @Override |
96 | | - public int available() throws IOException |
97 | | - { |
98 | | - return inner.available(); |
99 | | - } |
100 | | - |
101 | | - @Override |
102 | | - public void close() throws IOException |
103 | | - { |
104 | | - if (inner == null) |
105 | | - return; |
106 | | - |
107 | | - try |
108 | | - { |
109 | | - inner.close(); |
110 | | - try |
111 | | - { |
112 | | - finalRs.close(); |
113 | | - stmt.close(); |
114 | | - } |
115 | | - catch (SQLException e) |
116 | | - { |
117 | | - throw new IOException(e); |
118 | | - } |
119 | | - } |
120 | | - finally |
121 | | - { |
122 | | - inner = null; |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - public void mark(int readlimit) |
128 | | - { |
129 | | - inner.mark(readlimit); |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public void reset() throws IOException |
134 | | - { |
135 | | - inner.reset(); |
136 | | - } |
| 72 | + if (rs.next()) |
| 73 | + { |
| 74 | + InputStream in = rs.getBinaryStream(1); |
| 75 | + bout = new ByteArrayOutputStream(); |
137 | 76 |
|
138 | | - @Override |
139 | | - public boolean markSupported() |
140 | | - { |
141 | | - return inner.markSupported(); |
142 | | - } |
143 | | - }; |
144 | | - } |
145 | | - else |
146 | | - { |
147 | | - rs.close(); |
148 | | - stmt.close(); |
| 77 | + byte[] buffer = new byte[8192]; |
| 78 | + int count; |
149 | 79 |
|
150 | | - try (PreparedStatement stmt2 = conn.prepareStatement("select child from sqlj.list_dir(?)")) |
| 80 | + while ((count = in.read(buffer)) != -1) |
| 81 | + bout.write(buffer, 0, count); |
| 82 | + } |
| 83 | + else |
151 | 84 | { |
152 | | - stmt2.setString(1, urlStr); |
153 | | - |
154 | | - try (ResultSet rs2 = stmt2.executeQuery()) |
| 85 | + try (PreparedStatement stmt2 = conn.prepareStatement("select child from sqlj.list_dir(?)")) |
155 | 86 | { |
156 | | - if (rs2.next()) |
157 | | - { |
158 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
159 | | - PrintWriter writer = new PrintWriter(out); |
| 87 | + stmt2.setString(1, urlStr); |
160 | 88 |
|
161 | | - do |
| 89 | + try (ResultSet rs2 = stmt2.executeQuery()) |
| 90 | + { |
| 91 | + if (rs2.next()) |
162 | 92 | { |
163 | | - writer.println(rs2.getString(1)); |
164 | | - } while (rs2.next()); |
| 93 | + bout = new ByteArrayOutputStream(); |
| 94 | + PrintWriter writer = new PrintWriter(bout); |
165 | 95 |
|
166 | | - writer.close(); |
| 96 | + do |
| 97 | + { |
| 98 | + writer.println(rs2.getString(1)); |
| 99 | + } while (rs2.next()); |
167 | 100 |
|
168 | | - return new ByteArrayInputStream(out.toByteArray()); |
| 101 | + writer.flush(); |
| 102 | + } |
169 | 103 | } |
170 | 104 | } |
171 | | - } |
172 | 105 |
|
173 | | - throw new IOException(String.format("Resource '%s' has not been found on the database.", url)); |
| 106 | + throw new IOException( |
| 107 | + String.format("Resource '%s' has not been found on the database.", url)); |
| 108 | + } |
174 | 109 | } |
175 | 110 | } |
176 | | - catch (SQLException e) |
177 | | - { |
178 | | - if (rs != null) |
179 | | - rs.close(); |
180 | | - stmt.close(); |
181 | | - throw e; |
182 | | - } |
183 | 111 | } |
184 | 112 | catch (SQLException e) |
185 | 113 | { |
186 | 114 | log.error("Error retrieving resource or class from the database", e); |
187 | 115 | throw new IOException(e); |
188 | 116 | } |
189 | 117 | } |
| 118 | + |
| 119 | + @Override |
| 120 | + public InputStream getInputStream() throws IOException |
| 121 | + { |
| 122 | + return new ByteArrayInputStream(bout.toByteArray()); |
| 123 | + } |
190 | 124 | } |
0 commit comments