Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.sound;
24 
27 import java.io.BufferedInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.NoSuchFileException;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class AudioFileLoader {
42 
47  @Nullable
48  private final DebugWriter debugSound;
49 
55  public AudioFileLoader(@Nullable final DebugWriter debugSound) {
56  this.debugSound = debugSound;
57  }
58 
66  @NotNull
67  public InputStream getInputStream(@NotNull final CharSequence name) throws IOException {
68  final StringBuilder sb = new StringBuilder("resource/sounds/");
69  for (int i = 0; i < 64 && i < name.length(); i++) {
70  final char ch = name.charAt(i);
71  if (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z')) {
72  sb.append(ch);
73  } else {
74  sb.append('_');
75  }
76  }
77  sb.append(".mp3");
78  final String resource = sb.toString();
79 
80  final Path localFile = Paths.get(resource).toAbsolutePath().normalize();
81  try {
82  final InputStream inputStream = Files.newInputStream(localFile);
83  if (debugSound != null) {
84  debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+localFile);
85  }
86  return new BufferedInputStream(inputStream);
87  } catch (final NoSuchFileException ignored) {
88  if (debugSound != null) {
89  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+localFile);
90  }
91  }
92 
93  final Path file = Filenames.getSettingsFile("sounds").resolve(resource);
94  try {
95  final InputStream inputStream = Files.newInputStream(file);
96  if (debugSound != null) {
97  debugSound.debugProtocolWrite("resource: ["+resource+"] found as file:"+file);
98  }
99  return new BufferedInputStream(inputStream);
100  } catch (final NoSuchFileException ignored) {
101  if (debugSound != null) {
102  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as file:"+file);
103  }
104  }
105 
106  final InputStream inputStream = AudioFileLoader.class.getClassLoader().getResourceAsStream(resource);
107  if (inputStream != null) {
108  if (debugSound != null) {
109  debugSound.debugProtocolWrite("resource: ["+resource+"] found as rsrc:"+resource);
110  }
111  return new BufferedInputStream(inputStream);
112  }
113  if (debugSound != null) {
114  debugSound.debugProtocolWrite("resource: ["+resource+"] not found as rsrc:"+resource);
115  }
116 
117  throw new IOException("resource "+resource+" does not exist");
118  }
119 
120 }
com.realtime.crossfire.jxclient.sound.AudioFileLoader.debugSound
final DebugWriter debugSound
The writer for logging sound related information or.
Definition: AudioFileLoader.java:48
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.settings.Filenames.getSettingsFile
static Path getSettingsFile()
Returns the main settings file.
Definition: Filenames.java:96
com.realtime.crossfire.jxclient.sound.AudioFileLoader
Locates audio files.
Definition: AudioFileLoader.java:41
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.util.DebugWriter.debugProtocolWrite
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
Definition: DebugWriter.java:68
com.realtime.crossfire.jxclient.settings.Filenames
Utility class to return references to settings files.
Definition: Filenames.java:37
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.util.DebugWriter
Writer debug information to a log file.
Definition: DebugWriter.java:36
com.realtime.crossfire.jxclient.sound.AudioFileLoader.getInputStream
InputStream getInputStream(@NotNull final CharSequence name)
Returns an input stream for an audio file.
Definition: AudioFileLoader.java:67
com.realtime.crossfire.jxclient.sound.AudioFileLoader.AudioFileLoader
AudioFileLoader(@Nullable final DebugWriter debugSound)
Private constructor to prevent instantiation.
Definition: AudioFileLoader.java:55