jaulib v1.3.0
Jau Support Library (C++, Java, ..)
AssetURLConnection.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2012 Gothel Software e.K.
5 * Copyright (c) 2013 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.net;
27
28import java.io.IOException;
29import java.net.JarURLConnection;
30import java.net.URL;
31
32/**
33 * See base class {@link PiggybackURLConnection} for motivation.
34 *
35 * <p>
36 * <i>asset</i> resource location protocol connection.
37 * </p>
38 *
39 * <p>
40 * See {@link AssetURLContext#resolve(String)} how resources are being resolved.
41 * </p>
42 *
43 * <h3>Example:</h3>
44 *
45 * Assuming the plain <i>asset entry</i> <b><code>test/lala.txt</code></b> is being resolved by
46 * a class <code>test.LaLaTest</code>, ie. using the <i>asset aware</i> ClassLoader,
47 * one would use the following <i>asset</i> aware filesystem layout:
48 *
49 * <pre>
50 * test/LaLaTest.class
51 * assets/test/lala.txt
52 * </pre>
53 *
54 * The above maybe on a plain filesystem, or within a JAR or an APK file,
55 * e.g. <code>jogamp.test.apk</code>.
56 *
57 * The above would result in the following possible URLs
58 * reflecting the plain and resolved state of the <i>asset URL</i>:
59 * <pre>
60 * 0 Entry test/lala.txt
61 * 1 Plain asset:test/lala.txt
62 * 2 Resolved asset:jar:file:/data/app/jogamp.test.apk!/assets/test/lala.txt
63 * </pre>
64 *
65 * <p>
66 * The sub protocol URL of the resolved <i>asset</i>
67 * <pre>
68 * 3 Sub-URL jar:file:/data/app/jogamp.test.apk!/assets/test/lala.txt
69 * </pre>
70 * can be retrieved using {@link #getSubProtocol()}.
71 * </p>
72 *
73 * In all above cases, the <i>asset entry</i> is <b><code>test/lala.txt</code></b>,
74 * which can be retrieved via {@link #getEntryName()}.
75 *
76 * <p>
77 * <h3>General Implementation Notes:</h3>
78 * An <i>asset</i> URL is resolved using {@link AssetURLContext#getClassLoader()}.{@link ClassLoader#getResource(String) getResource(String)},
79 * hence the only requirement for an implementation is to have an <i>asset</i> aware ClassLoader
80 * as described in {@link AssetURLContext#getClassLoader()}.
81 * </p>
82 * <p>
83 * <h3>Warning:</h3>
84 * Since the <i>asset</i> protocol is currently not being implemented
85 * on all platform with an appropriate ClassLoader, a user shall not create the <i>asset</i> URL manually.<br>
86 * </p>
87 *
88 * <h3>Android Implementation Notes:</h3>
89 * <p>
90 * The Android ClassLoader {@link jogamp.android.launcher.AssetDexClassLoader}
91 * resolves the resource as an <i>asset</i> URL in it's {@link ClassLoader#findResource(String)} implementation.</p>
92 * <p>
93 * Currently we attach our <i>asset</i> {@link java.net.URLStreamHandlerFactory}
94 * to allow {@link java.net.URL} to handle <i>asset</i> URLs via our <i>asset</i> {@link java.net.URLStreamHandler} implementation.
95 * </p>
96 */
97public class AssetURLConnection extends PiggybackURLConnection<AssetURLContext> {
98
99 public AssetURLConnection(final URL url, final AssetURLContext implHelper) {
100 super(url, implHelper);
101 }
102
103 @Override
104 public String getEntryName() throws IOException {
105 if(!connected) {
106 throw new IOException("not connected");
107 }
108
109 final String urlPath ;
110 if(subConn instanceof JarURLConnection) {
111 urlPath = ((JarURLConnection)subConn).getEntryName();
112 } else {
113 urlPath = subConn.getURL().getPath();
114 }
115
116 if(urlPath.startsWith(AssetURLContext.assets_folder)) {
117 return urlPath.substring(AssetURLContext.assets_folder.length());
118 } else {
119 return urlPath;
120 }
121 }
122
123}
See base class PiggybackURLConnection for motivation.
AssetURLConnection(final URL url, final AssetURLContext implHelper)
See PiggybackURLConnection for description and examples.
static final String assets_folder
The optional asset folder name with ending slash assets/.
Generic resource location protocol connection, using another sub-protocol as the vehicle for a piggyb...