00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.sound;
00023
00024 import com.realtime.crossfire.jxclient.settings.Filenames;
00025 import com.realtime.crossfire.jxclient.util.DebugWriter;
00026 import java.io.File;
00027 import java.io.FileInputStream;
00028 import java.io.FileNotFoundException;
00029 import java.io.IOException;
00030 import java.io.InputStream;
00031 import org.jetbrains.annotations.NotNull;
00032 import org.jetbrains.annotations.Nullable;
00033
00038 public class AudioFileLoader {
00039
00044 @Nullable
00045 private final DebugWriter debugSound;
00046
00052 public AudioFileLoader(@Nullable final DebugWriter debugSound) {
00053 this.debugSound = debugSound;
00054 }
00055
00064 @NotNull
00065 public InputStream getInputStream(@Nullable final String name, @NotNull final String action) throws IOException {
00066 @Nullable final IOException savedException;
00067 if (name != null) {
00068 try {
00069 return getResource(name+"/"+action);
00070 } catch (final IOException ex) {
00071 savedException = ex;
00072 }
00073 } else {
00074 savedException = null;
00075 }
00076
00077 try {
00078 return getResource(action);
00079 } catch (final IOException ex) {
00080 throw savedException != null ? savedException : ex;
00081 }
00082 }
00083
00090 @NotNull
00091 private InputStream getResource(@NotNull final String name) throws IOException {
00092 final String resource = "resource/sounds/"+name+".wav";
00093
00094 @Nullable File localFile;
00095 try {
00096 localFile = new File(resource).getCanonicalFile();
00097 } catch (final IOException ex) {
00098 if (debugSound != null) {
00099 debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+resource+" ("+ex.getMessage()+")");
00100 }
00101 localFile = null;
00102 }
00103 if (localFile != null) {
00104 try {
00105 final InputStream inputStream = new FileInputStream(localFile);
00106 if (debugSound != null) {
00107 debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+localFile);
00108 }
00109 return inputStream;
00110 } catch (final FileNotFoundException ignored) {
00111 if (debugSound != null) {
00112 debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+localFile);
00113 }
00114 }
00115 }
00116
00117 final File file = new File(Filenames.getSettingsFile("sounds"), resource);
00118 try {
00119 final InputStream inputStream = new FileInputStream(file);
00120 if (debugSound != null) {
00121 debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+file);
00122 }
00123 return inputStream;
00124 } catch (final FileNotFoundException ignored) {
00125 if (debugSound != null) {
00126 debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+file);
00127 }
00128 }
00129
00130 final InputStream inputStream = AudioFileLoader.class.getClassLoader().getResourceAsStream(resource);
00131 if (inputStream != null) {
00132 if (debugSound != null) {
00133 debugSound.debugProtocolWrite("resource: ["+resource+"] found as rsrc:"+resource);
00134 }
00135 return inputStream;
00136 }
00137 if (debugSound != null) {
00138 debugSound.debugProtocolWrite("resource: ["+resource+"] not found as rsrc:"+resource);
00139 }
00140
00141 throw new IOException("resource "+resource+" does not exist");
00142 }
00143
00144 }