0

I have a project (in Eclipse, but that doesn't matter) with a hierarchy as follows:

-src
---Start.java
---resources
-----media
-------intro.wav
-----textures
-------logo.png
-------tiles.abotm

In Start.java, I'm trying to get tiles.abotm as an InputStream using Class.getResourceAsStream(String) as such:

public class Start
{
  public static void main(String[] args)
  {
    try
    {
      InputStream in = Start.class.getResourceAsStream(
                           "/resources/textures/tiles.abotm");
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}

Simple enough, right? Unfortunately, no. The InputStream is completely empty with a size of 0. I've also tried opening a FileInputStream directly to the absolute location of tiles.abotm, but I get the same thing! I know the file is not empty. In fact, it has 2,257 bytes, according to Windows, Eclipse, and the File object used to create the FileInputStream mentioned previously. Also according to the File object, it is readable, writable, it exists, it is not a directory, and the name of it is tiles.abotm. So, if the File object can read it, why can't it be opened up in an InputStream??

--EDIT-- I forgot to mention that I have another file in the textures directory called logo.png which I am able to open and read in the exact same manner with no problem at all. It is only this file.

--In reply to fge, this is the actual code: Loader.loadTextureMap("/resources/textures/tiles.abotm");//This is called in a separate method in a separate class.

public class Loader{
  public static TextureMap loadTextureMap(String texMap){
    DataInputStream dis = new DataInputStream(
                 Start.class.getResourceAsStream(texMap));
    //It then goes on to read it, but I've determined that at this point,
                there is nothing in this DataInputStream.
  }
}
Steven
  • 1,709
  • 3
  • 17
  • 27
  • When you say "absolute location", do you mean starting from /? – fge Jun 17 '13 at 06:37
  • Have a look at [How to really read text file from classpath in Java](http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java) – neo108 Jun 17 '13 at 06:38
  • I believe Eclipse requires it's resources to exist in the resource folder and not the source folder. You could try using something like `/src/resources/textures/tiles.abotm` as a test, but I'd change before you release it – MadProgrammer Jun 17 '13 at 06:39
  • 2
    Side note: `.getResourceAsStream()` will never throw an exception; if the resource does not exist, it returns `null`. – fge Jun 17 '13 at 06:39
  • Guys, look at the edit, please. – Steven Jun 17 '13 at 06:40
  • get your /resources folder out of /src folder – voidMainReturn Jun 17 '13 at 06:40
  • @fge Yes, but it's not returning null, it's returning empty. – Steven Jun 17 '13 at 06:40
  • Try and open the file using `RandomAccessFile(Paths.get("thepath").toFile())` and see the value of `.length()`, what does it return? – fge Jun 17 '13 at 06:43
  • @fge Also, by absolute path, I mean the one you get from calling the getAbsolutePath() method on the file. For me, it's `C:/Users/Steven/Eclipse Workspace/Java/Project/src/resources/textures/tiles.abotm` – Steven Jun 17 '13 at 06:43
  • @fge The `RandomAccessFile` also gave me 2257. – Steven Jun 17 '13 at 06:47
  • OK, can you show the code you are using to read the file, please? Aren't you by any chance using a `Reader`? – fge Jun 17 '13 at 06:47
  • @fge I am not using a reader. I've added the actual code to the question. – Steven Jun 17 '13 at 06:56
  • Look at my answer -- why it is not really one, this comment thread is becoming too long – fge Jun 17 '13 at 06:56

1 Answers1

1

After a lot of discussion, the code that works for the OP:

final byte[] buf = new byte[1024]; // or other
final URL url = Start.class.getResource("whatever");
// check for url == null

InputStream in;
ByteArrayOutputStream out;

// I really wish this syntax was something else, it sucks
try (
    in = url.openStream();
    out = new ByteArrayOutputStream();
) {
    int count;
    while ((count = in.read(buf)) != -1)
        out.write(buf, 0, count);
    out.flush();
} catch (IOException e) {
    // handle e here
}

final ByteBuffer buffer = ByteBuffer.wrap(out.toByteArray());
// use the buffer
fge
  • 119,121
  • 33
  • 254
  • 329
  • I did that, and it worked, but, this file is going to be packed up in the jar. So, I can't read it that way in the finale program. – Steven Jun 17 '13 at 06:59
  • OK, how do you build that texture map stuff? – fge Jun 17 '13 at 07:00
  • I have made my own program to do so. It's all based on my own version of serializable objects (called BSOs - Basic Serializable Objects) and all this file is is two integers and an image. Is that the issue? Is the byte-value of 0 at the very beginning messing it up? Because, in this file, the first 8 bytes are 0 0 0 16 0 0 0 16. – Steven Jun 17 '13 at 07:03
  • Thanks a ton! It worked! Now, my question is, why wouldn't it work the other way? – Steven Jun 17 '13 at 07:21
  • 1
    Good question! Right now I cannot tell... I haven't used `DataInputStream` for _ages_ since I now systematically use NIO ;) – fge Jun 17 '13 at 07:23