Problem solved! (may not be the best way).
My problem is that I took the method
public static Image uploadImage (String path)
and rewrote part of it for audio.
In the method for images, there is a string for unpacking the JAR.
InputStream fileInputStreamReader = (jar.getInputStream (jar.getEntry (path)));
Next, I wrote the code for audio. It didn't work directly: it was not the same for
InputStream sourceSound = new BufferedInputStream (fileInputStreamReader);
... ...
return sourceSound;
Next step - I added:
InputStream fileInputStreamReader =(jar.getInputStream(jar.getEntry(path)));
byte[] byteArray = new byte[fileInputStreamReader.available()];
fileInputStreamReader.read(byteArray);
InputStream newInputStreamFromArray = new BufferedInputStream(
new ByteArrayInputStream(byteArray));
sourceSound = newInputStreamFromArray;
And it worked! (I thought so). After posting the question here, I started to think that not all inputStream are written in byte[] byteArray.
I made a copy of the byteArray and wrote it to a file "H:/1.wav".
And this is what I saw in the file:
RIFFp[) WAVEfmt D? ± data?Z) ?1V?E?R??V??H .
.....................................................................................
.....................................................................................
And etс. The data was cut off. There was no content.
Working code for images:
BufferedImage sourceImage = null;
InputStream fileInputStreamReader = jar.getInputStream(jar.getJarEntry(path));
sourceImage = ImageIO.read(fileInputStreamReader);//magic ImageIO.read () !!!!
Likewise does not work for audio:
InputStream fileInputStreamReader =(jar.getInputStream(jar.getEntry(path)));
byte[] byteArray = new byte[fileInputStreamReader.available()];
fileInputStreamReader.read(byteArray);
I guess I didn’t learn Java well =)
So I solved the problem now like this:
InputStream fileInputStreamReader =(jar.getInputStream(jar.getEntry(path)));
byte[] byteArray = new byte[fileInputStreamReader.available()];
int i = 0;
int byteRead;
while ((byteRead = fileInputStreamReader.read()) != -1) {
byteArray[i] = (byte) byteRead;
i++;
}
InputStream newInputStreamFromArray = new BufferedInputStream(new ByteArrayInputStream(byteArray));
Long and short audios are now read from the JAR normally.
If someone knows how to do it better - I will be glad to hear the answer.