Home > Enterprise >  Sound distortion when extracting from Jar
Sound distortion when extracting from Jar

Time:12-25

I have sounds in my jar file directory. I need to use these sounds and I am trying to extract them using this method:

String charset = "ISO-8859-1";
public void extractSounds(String pathIn, String pathOut) throws IOException {
    BufferedReader r = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(pathIn), charset));
    String line = r.readLine();
    String result = null;
    FileOutputStream fos = new FileOutputStream(pathOut);
    while(line != null) {
        if(result != null) {
            result  = "\r"   line;
            line = r.readLine();
        } else {
            result = line;
            line = r.readLine();
        }
    }
    fos.write(result.getBytes(charset));
}}

But when I extract the sounds they get distorted and I don't know what the problem is, because it basically just copies the file. Sounds: Original, Extracted

I would be very grateful if you could help me find a solution or suggest another method to extract the sound files.

CodePudding user response:

Don't assume you are reading text. You should not try to mutate the data. Just copy it in chunks.

Try something like

InputStream in = ...;
ByteArrayOutputStream out = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
    out.write(buffer, 0, bytesRead);
}
  • Related