jaulib v1.3.0
Jau Support Library (C++, Java, ..)
PiggybackURLConnection.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.io.InputStream;
30import java.net.URL;
31import java.net.URLConnection;
32
33/**
34 * Generic resource location protocol connection,
35 * using another sub-protocol as the vehicle for a piggyback protocol.
36 * <p>
37 * The details of the sub-protocol can be queried using {@link #getSubProtocol()}.
38 * </p>
39 * <p>
40 * See example in {@link AssetURLConnection}.
41 * </p>
42 */
43public abstract class PiggybackURLConnection<I extends PiggybackURLContext> extends URLConnection {
44 protected URL subUrl;
45 protected URLConnection subConn;
46 protected I context;
47
48 /**
49 * @param url the specific URL for this instance
50 * @param context the piggyback context, defining state independent code and constants
51 */
52 protected PiggybackURLConnection(final URL url, final I context) {
53 super(url);
54 this.context = context;
55 }
56
57 /**
58 * <p>
59 * Resolves the URL via {@link PiggybackURLContext#resolve(String)},
60 * see {@link AssetURLContext#resolve(String)} for an example.
61 * </p>
62 *
63 * {@inheritDoc}
64 */
65 @Override
66 public synchronized void connect() throws IOException {
67 if(!connected) {
68 subConn = context.resolve(url.getPath());
69 subUrl = subConn.getURL();
70 connected = true;
71 }
72 }
73
74 @Override
75 public InputStream getInputStream() throws IOException {
76 if(!connected) {
77 throw new IOException("not connected");
78 }
79 return subConn.getInputStream();
80 }
81
82 /**
83 * Returns the <i>entry name</i> of the asset.
84 * <pre>
85 * Plain asset:test/lala.txt
86 * Resolved asset:jar:file:/data/app/jogamp.test.apk!/assets/test/lala.txt
87 * Result test/lala.txt
88 * </pre>
89 * @throws IOException is not connected
90 **/
91 public abstract String getEntryName() throws IOException;
92
93 /**
94 * Returns the resolved <i>sub protocol</i> of the asset or null, ie:
95 * <pre>
96 * Plain asset:test/lala.txt
97 * Resolved asset:jar:file:/data/app/jogamp.test.apk!/assets/test/lala.txt
98 * Result jar:file:/data/app/jogamp.test.apk!/assets/test/lala.txt
99 * </pre>
100 *
101 * @throws IOException is not connected
102 */
103 public URL getSubProtocol() throws IOException {
104 if(!connected) {
105 throw new IOException("not connected");
106 }
107 return subUrl;
108 }
109}
Generic resource location protocol connection, using another sub-protocol as the vehicle for a piggyb...
abstract String getEntryName()
Returns the entry name of the asset.
URL getSubProtocol()
Returns the resolved sub protocol of the asset or null, ie:
PiggybackURLConnection(final URL url, final I context)
See PiggybackURLConnection for description and examples.