Cannot create InputStream from a text file in iOS

89 views
Skip to first unread message

rbr...@gmail.com

unread,
Sep 28, 2013, 6:49:32 AM9/28/13
to codenameone...@googlegroups.com
Hi,
I looked for an answer but it seems I am the only one to have this problem.
My goal is to create some objects parsing a text file(that should be inside the jar/apk/ipa package for distribution purpose) to pass arguments to the constructor; this is what I do:

private void createObjects() {
       
String[] stringhe = new String[4];
        splashPrint
("...init InputStream...");//set txt to a Label by Display.getInstance().callSeriallyAndWait(R)
       
InputStream objList = Display.getInstance().getResourceAsStream(InputStream.class, "/objs");
        splashPrint
("...init Reader...");
       
InputStreamReader isr = new InputStreamReader(objList);
        splashPrint
("...read from InputStream...");
       
for(int i = 0; i < objs.length; i++){
           
for (int j = 0; j < stringhe.length; j++) {
                splashPrint
("...try reading String[" + i + "][" + j + "]...");
               
StringBuffer sb = new StringBuffer();
               
boolean separator = false;
               
while (!separator) {
                   
try {
                       
char c = (char) isr.read();
                       
if(c == '#'){
                           
while(c != '\n'){
                                c
= (char) isr.read();
                           
}
                            c
= (char) isr.read();
                       
}
                       
if (c == ',' || c == '\n' || c == '\u001a') {
                            separator
= true;
                       
} else {
                            sb
.append(c);
                       
}
                   
} catch (IOException e) {
                        e
.printStackTrace();
                   
}

               
}
                stringhe
[j] = sb.toString();
                splashPrint
("...finish reading String[" + i + "][" + j + "]...");
           
}
            splashPrint
("...loading Image...");
           
Image immagine = null;
           
try {
               
if (stringhe[2].equals("0")) {
                    immagine
= Image.createImage("/" + stringhe[0]
                           
+ stringhe[1] + ".png");
               
}else if (stringhe[2].equals("1")){
                    immagine
= Image.createImage("/" + stringhe[0]
                           
+ stringhe[1] + "m" + ".png");
               
}else {
                    immagine
= Image.createImage("/" + stringhe[0]
                           
+ stringhe[1] + "f" + ".png");
               
}
           
} catch (IOException e) {
                e
.printStackTrace();
               
Display.getInstance().exitApplication();
           
}
            splashPrint
("...building " + i + " - ...");
            objs
[i] = new Obj(stringhe[0], stringhe[1], Integer.parseInt(stringhe[2]), Boolean.parseBoolean(stringhe[3]), immagine);
           
            splashPrint
("...building " + i + " - " + objs[i].toString() + "DONE!");
       
}
       
try {
            isr
.close();
       
} catch (IOException e1) {
            e1
.printStackTrace();
       
}
       
try {
            objList
.close();
       
} catch (IOException e) {
            e
.printStackTrace();
       
}
       
   
}

In simulator and android2.3, everything works fine while my iPod Touch 4g(iOS 6.1.3) stops showing me "...init InputStream..." on the debug Label and a Dialog in which compare a message about a java.util.NoSuchElementException.
The strange thing is that if I substitute the file parsing with an array of String, everything works well, even the Image loading, so I suppose there is something wrong when I try to create the input stream from the file.

Many thanks in advance.

Shai Almog

unread,
Sep 28, 2013, 3:04:10 PM9/28/13
to codenameone...@googlegroups.com
Hi,
I suggest calling the file objs.txt and you should use getClass() rather than InputStream.class.
iOS doesn't really allow opening a file in the jar, its got a rather complex lookup API.

Fabrizio Grassi

unread,
Sep 29, 2013, 11:31:43 AM9/29/13
to codenameone...@googlegroups.com
Hi Shai,
first of all I would like to thank you for your support!
then I would like to remark, for all the community, that I tried first to substitute "InputStream.class" with "getClass()" and it didn't work, then I renamed the file "objs" in "objs.txt" and it worked!
I wonder why should the file be named as "*.txt" but now it is ok, the only strange behavior now is that the debugging Label neither appear nor update the text during the whole process(this is important only for debug), any suggestion?

thank you!

Shai Almog

unread,
Sep 29, 2013, 2:49:24 PM9/29/13
to codenameone...@googlegroups.com
Hi Fabrizio,
thanks that is much appreciated.

The txt thing is an issue with iOS. This is the code that actually loads a resource file in iOS:
    NSString* path = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:name] ofType:[NSString stringWithUTF8String:type]];

Notice that it expects a name and a type with the name being objs and type being txt this works. The reasoning is that you have localization and image resolution independence built into the file loading logic in iOS. Unfortunately this behavior isn't portable.

I'm assuming you are invoking the method on the EDT (which you shouldn't for a long running task). The EDT is the thread that paints and handles events so you just won't see refreshes if you block it.
If you are doing this off the EDT then you need to wrap the set text calls in call serially.

Reply all
Reply to author
Forward
0 new messages