Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/paulscode/sound/FilenameURL.java
8644 views
1
package paulscode.sound;
2
3
import java.net.URL;
4
5
/**
6
* The FilenameURL class is designed to associate a String filename/identifier
7
* with a URL. Handles either case where user supplies a String path or user
8
* supplies a URL instance.
9
*<br><br>
10
*<b><i> SoundSystem License:</b></i><br><b><br>
11
* You are free to use this library for any purpose, commercial or otherwise.
12
* You may modify this library or source code, and distribute it any way you
13
* like, provided the following conditions are met:
14
*<br>
15
* 1) You may not falsely claim to be the author of this library or any
16
* unmodified portion of it.
17
*<br>
18
* 2) You may not copyright this library or a modified version of it and then
19
* sue me for copyright infringement.
20
*<br>
21
* 3) If you modify the source code, you must clearly document the changes
22
* made before redistributing the modified source code, so other users know
23
* it is not the original code.
24
*<br>
25
* 4) You are not required to give me credit for this library in any derived
26
* work, but if you do, you must also mention my website:
27
* http://www.paulscode.com
28
*<br>
29
* 5) I the author will not be responsible for any damages (physical,
30
* financial, or otherwise) caused by the use if this library or any part
31
* of it.
32
*<br>
33
* 6) I the author do not guarantee, warrant, or make any representations,
34
* either expressed or implied, regarding the use of this library or any
35
* part of it.
36
* <br><br>
37
* Author: Paul Lamb
38
* <br>
39
* http://www.paulscode.com
40
* </b>
41
*/
42
public class FilenameURL
43
{
44
/**
45
* Processes status messages, warnings, and error messages.
46
*/
47
private SoundSystemLogger logger;
48
49
/**
50
* Filename or identifier for the file.
51
*/
52
private String filename = null;
53
54
/**
55
* URL interface to the file.
56
*/
57
private URL url = null;
58
59
/**
60
* Constructor: Saves handles to the url and identifier. The identifier should
61
* look like a filename, and it must have the correct extension so SoundSystem
62
* knows what format to use for the file referenced by the URL instance.
63
* @param url URL interface to a file.
64
* @param identifier Identifier (filename) for the file.
65
*/
66
public FilenameURL( URL url, String identifier )
67
{
68
// grab a handle to the message logger:
69
logger = SoundSystemConfig.getLogger();
70
71
filename = identifier;
72
this.url = url;
73
}
74
75
/**
76
* Constructor: Saves a handle to the filename (used later to generate a URL
77
* instance). The file may either be located within the
78
* JAR or at an online location. If the file is online, filename must begin
79
* with "http://", since that is how SoundSystem recognizes URL names.
80
* @param filename Name of the file.
81
*/
82
public FilenameURL( String filename )
83
{
84
// grab a handle to the message logger:
85
logger = SoundSystemConfig.getLogger();
86
87
this.filename = filename;
88
url = null;
89
}
90
91
/**
92
* Returns the filename/identifier.
93
* @return Filename or identifier for the file.
94
*/
95
public String getFilename()
96
{
97
return filename;
98
}
99
100
/**
101
* Returns the URL interface to the file. If a URL was not originally specified
102
* in the constructor, then the first time this method is called it creates a
103
* URL instance using the previously specified filename.
104
* @return URL interface to the file.
105
*/
106
public URL getURL()
107
{
108
if( url == null )
109
{
110
// Check if the file is online or inside the JAR:
111
if( filename.matches( SoundSystemConfig.PREFIX_URL ) )
112
{
113
// Online
114
try
115
{
116
url = new URL( filename );
117
}
118
catch( Exception e )
119
{
120
errorMessage( "Unable to access online URL in " +
121
"method 'getURL'" );
122
printStackTrace( e );
123
return null;
124
}
125
}
126
else
127
{
128
// Inside the JAR
129
url = getClass().getClassLoader().getResource(
130
SoundSystemConfig.getSoundFilesPackage() + filename );
131
}
132
}
133
return url;
134
}
135
136
/**
137
* Prints an error message.
138
* @param message Message to print.
139
*/
140
private void errorMessage( String message )
141
{
142
logger.errorMessage( "MidiChannel", message, 0 );
143
}
144
145
/**
146
* Prints an exception's error message followed by the stack trace.
147
* @param e Exception containing the information to print.
148
*/
149
private void printStackTrace( Exception e )
150
{
151
logger.printStackTrace( e, 1 );
152
}
153
}
154
155