Crossfire JXClient, Trunk  R20561
AudioFileLoader.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.sound;
23 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
38 public class AudioFileLoader {
39 
44  @Nullable
45  private final DebugWriter debugSound;
46 
52  public AudioFileLoader(@Nullable final DebugWriter debugSound) {
53  this.debugSound = debugSound;
54  }
55 
64  @NotNull
65  public InputStream getInputStream(@Nullable final String name, @NotNull final String action) throws IOException {
66  @Nullable final IOException savedException;
67  if (name == null) {
68  savedException = null;
69  } else {
70  try {
71  return getResource(name+"/"+action);
72  } catch (final IOException ex) {
73  savedException = ex;
74  }
75  }
76 
77  try {
78  return getResource(action);
79  } catch (final IOException ex) {
80  throw savedException == null ? ex : savedException;
81  }
82  }
83 
90  @NotNull
91  private InputStream getResource(@NotNull final String name) throws IOException {
92  final String resource = "resource/sounds/"+name+".wav";
93 
94  @Nullable File localFile;
95  try {
96  localFile = new File(resource).getCanonicalFile();
97  } catch (final IOException ex) {
98  if (debugSound != null) {
99  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+resource+" ("+ex.getMessage()+")");
100  }
101  localFile = null;
102  }
103  if (localFile != null) {
104  try {
105  final InputStream inputStream = new FileInputStream(localFile);
106  if (debugSound != null) {
107  debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+localFile);
108  }
109  return inputStream;
110  } catch (final FileNotFoundException ignored) {
111  if (debugSound != null) {
112  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+localFile);
113  }
114  }
115  }
116 
117  final File file = new File(Filenames.getSettingsFile("sounds"), resource);
118  try {
119  final InputStream inputStream = new FileInputStream(file);
120  if (debugSound != null) {
121  debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+file);
122  }
123  return inputStream;
124  } catch (final FileNotFoundException ignored) {
125  if (debugSound != null) {
126  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+file);
127  }
128  }
129 
130  final InputStream inputStream = AudioFileLoader.class.getClassLoader().getResourceAsStream(resource);
131  if (inputStream != null) {
132  if (debugSound != null) {
133  debugSound.debugProtocolWrite("resource: ["+resource+"] found as rsrc:"+resource);
134  }
135  return inputStream;
136  }
137  if (debugSound != null) {
138  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as rsrc:"+resource);
139  }
140 
141  throw new IOException("resource "+resource+" does not exist");
142  }
143 
144 }
final DebugWriter debugSound
The writer for logging sound related information or.
Utility class to return references to settings files.
Definition: Filenames.java:34
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
Writer debug information to a log file.
AudioFileLoader(@Nullable final DebugWriter debugSound)
Private constructor to prevent instantiation.
static File getSettingsFile()
Returns the main settings file.
Definition: Filenames.java:93
InputStream getInputStream(@Nullable final String name, @NotNull final String action)
Returns an input stream for an audio file.
InputStream getResource(@NotNull final String name)
Returns an input stream for an audio file.