Debian Codegraph @

scripting/java/com/sun/star/script/framework/io/UCBStreamHandler.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package com.sun.star.script.framework.io;
20
21 import com.sun.star.io.XInputStream;
22 import com.sun.star.io.XOutputStream;
23 import com.sun.star.io.XTruncate;
24
25 import com.sun.star.script.framework.log.LogUtils;
26 import com.sun.star.script.framework.provider.PathUtils;
27
28 import com.sun.star.ucb.XSimpleFileAccess;
29
30 import com.sun.star.uno.UnoRuntime;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36
37 import java.net.URL;
38 import java.net.URLConnection;
39 import java.net.URLStreamHandler;
40
41 import java.util.HashMap;
42 import java.util.zip.ZipEntry;
43 import java.util.zip.ZipInputStream;
44
45 public class UCBStreamHandler extends URLStreamHandler {
46
47 public static final String separator = "/ucb/";
48
49 private final XSimpleFileAccess m_xSimpleFileAccess;
50 private final HashMap<String, InputStream> m_jarStreamMap = new HashMap<String, InputStream>(12);
51 private static String m_ucbscheme;
52
53 public UCBStreamHandler(String scheme, XSimpleFileAccess xSFA) {
54 LogUtils.DEBUG("UCBStreamHandler ctor, scheme = " + scheme);
55 UCBStreamHandler.m_ucbscheme = scheme;
56 this.m_xSimpleFileAccess = xSFA;
57 }
58
59 @Override
60 public void parseURL(URL url, String spec, int start, int limit) {
61
62 LogUtils.DEBUG("**XUCBStreamHandler, parseURL: " + url + " spec: "
63 + spec + " start: " + start + " limit: " + limit);
64
65 String file = url.getFile();
66
67 if (file == null)
68 file = spec.substring(start, limit);
69 else
70 file += spec.substring(start, limit);
71
72 LogUtils.DEBUG("**For scheme = " + m_ucbscheme);
73 LogUtils.DEBUG("**Setting path = " + file);
74 setURL(url, m_ucbscheme, null, -1, null, null, file, null, null);
75 }
76
77 @Override
78 public URLConnection openConnection(URL u) throws IOException {
79 return new UCBConnection(u);
80 }
81
82 private class UCBConnection extends URLConnection {
83
84 public UCBConnection(URL url) {
85 super(url);
86 }
87
88 @Override
89 public void connect() {
90 }
91
92 @Override
93 public InputStream getInputStream() throws IOException {
94 LogUtils.DEBUG("UCBConnectionHandler GetInputStream on " + url);
95 String sUrl = url.toString();
96
97 if (sUrl.lastIndexOf(separator) == -1) {
98 LogUtils.DEBUG("getInputStream straight file load");
99 return getFileStreamFromUCB(sUrl);
100 } else {
101 String path = sUrl.substring(0, sUrl.lastIndexOf(separator));
102
103 String file =
104 sUrl.substring(sUrl.lastIndexOf(separator) + separator.length());
105
106 LogUtils.DEBUG("getInputStream, load of file from another file eg. "
107 + file + " from " + path);
108
109 return getUCBStream(file, path);
110 }
111 }
112 @Override
113 public OutputStream getOutputStream() throws IOException {
114 LogUtils.DEBUG("UCBConnectionHandler getOutputStream on " + url);
115 OutputStream os = null;
116
117 try {
118 String sUrl = url.toString();
119
120 if (sUrl.lastIndexOf(separator) != -1) {
121 String path = sUrl.substring(0, sUrl.lastIndexOf(separator));
122
123 if (m_xSimpleFileAccess.isReadOnly(path)) {
124 throw new java.io.IOException("File is read only");
125 }
126
127 LogUtils.DEBUG("getOutputStream, create o/p stream for file eg. "
128 + path);
129
130 // we will only deal with simple file write
131 XOutputStream xos = m_xSimpleFileAccess.openFileWrite(path);
132
133 XTruncate xtrunc =
134 UnoRuntime.queryInterface(XTruncate.class, xos);
135
136 if (xtrunc != null) {
137 xtrunc.truncate();
138 }
139
140 os = new XOutputStreamWrapper(xos);
141 }
142
143 if (os == null) {
144 throw new IOException("Failed to get OutputStream for "
145 + sUrl);
146 }
147 } catch (com.sun.star.ucb.CommandAbortedException cae) {
148 LogUtils.DEBUG("caught exception: " + cae.toString() +
149 " getting writable stream from " + url);
150 IOException newEx = new IOException(cae.getMessage());
151 newEx.initCause(cae);
152 throw newEx;
153 } catch (com.sun.star.uno.Exception e) {
154 LogUtils.DEBUG("caught unknown exception: " + e.toString() +
155 " getting writable stream from " + url);
156 IOException newEx = new IOException(e.getMessage());
157 newEx.initCause(e);
158 throw newEx;
159 }
160
161 return os;
162 }
163 }
164
165 private InputStream getUCBStream(String file, String path) throws IOException {
166
167 InputStream is = null;
168 InputStream result = null;
169
170 try {
171 if (path.endsWith(".jar")) {
172 is = m_jarStreamMap.get(path);
173
174 if (is == null) {
175 is = getFileStreamFromUCB(path);
176 m_jarStreamMap.put(path, is);
177 } else {
178 try {
179 is.reset();
180 } catch (IOException e) {
181 is.close();
182 is = getFileStreamFromUCB(path);
183 m_jarStreamMap.put(path, is);
184 }
185 }
186
187 result = getFileStreamFromJarStream(file, is);
188 } else {
189 String fileUrl = PathUtils.make_url(path, file);
190 result = getFileStreamFromUCB(fileUrl);
191 }
192 } finally {
193 if (is != null) {
194 try {
195 is.close();
196 } catch (IOException ioe) {
197 LogUtils.DEBUG("Caught exception closing stream: " +
198 ioe.getMessage());
199 }
200 }
201 }
202
203 return result;
204 }
205
206 private InputStream getFileStreamFromJarStream(String file,
207 InputStream is) throws
208 IOException {
209
210 ZipInputStream zis = new ZipInputStream(is);
211
212 while (zis.available() != 0) {
213 ZipEntry entry = zis.getNextEntry();
214
215 if (entry != null && entry.getName().equals(file)) {
216 return zis;
217 }
218 }
219
220 return null;
221 }
222
223 private InputStream getFileStreamFromUCB(String path) throws IOException {
224
225 InputStream result = null;
226 XInputStream xInputStream = null;
227
228 try {
229 LogUtils.DEBUG("Trying to read from " + path);
230 xInputStream = m_xSimpleFileAccess.openFileRead(path);
231 LogUtils.DEBUG("sfa appeared to read file ");
232 byte[][] inputBytes = new byte[1][];
233
234 int sz = m_xSimpleFileAccess.getSize(path);
235
236 // TODO don't depend on result of available() or size()
237 // just read stream 'till complete
238 if (sz == 0 && xInputStream.available() > 0) {
239 sz = xInputStream.available();
240 }
241
242 LogUtils.DEBUG("size of file " + path + " is " + sz);
243 LogUtils.DEBUG("available = " + xInputStream.available());
244 inputBytes[0] = new byte[sz];
245
246 int ln = xInputStream.readBytes(inputBytes, sz);
247
248 if (ln != sz) {
249 throw new IOException(
250 "Failed to read " + sz + " bytes from XInputStream");
251 }
252
253 result = new ByteArrayInputStream(inputBytes[0]);
254 } catch (com.sun.star.io.IOException ex1) {
255 IOException ex2 = new IOException();
256 ex2.initCause(ex1);
257 throw ex2;
258 } catch (com.sun.star.uno.Exception ex1) {
259 IOException ex2 = new IOException();
260 ex2.initCause(ex1);
261 throw ex2;
262 } finally {
263 if (xInputStream != null) {
264 try {
265 xInputStream.closeInput();
266 } catch (Exception e2) {
267 LogUtils.DEBUG("Error closing XInputStream: "
268 + e2.getMessage());
269 }
270 }
271 }
272
273 return result;
274 }
275 }