InputStream and Reader

For the program that I'm writing, I'm redirecting the default streams, so I made extended InputStream, overrode ALL the functions, and used System.setIn.

However, a common class that I use for IO in my previous console programs use Readers. I figured out that I needed to return a

(char) -1

after my buffer has ended as the end-of-stream.

However, if I only return it for a set number of times, like 1 or 50000 times, the Reader won't accept that end-of-stream, and keeps reading! If I don't stop returning the end-of-stream, the stream can't be read for a second time (the Reader keeps returning end-of-streams)! How should I solve this?

[674 byte] By [atoxica] at [2007-11-26 12:59:03]
# 1
java.util.BigHammer should work :)
Leditiona at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 2

If you want to convert the the InputStreams to Readers then use InputStreamReader to wrap your InputStreams.

When the EOF the read method of InputStreams should return integer -1. I have no idear what the (char) -1 will be converted in to becouse to my knoledge there is no -1 character and also the InputStreams should return bytes not chars.

LRMKa at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 3
Doesn't work...Produces the same results.The class uses:new PushbackReader(new InputStreamReader(System.in));
atoxica at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 4

(char)-1 is 0xffffffff. (int)-1 is 0xffffffffffffffff. Bit of a difference. Code that expects the latter, i.e. everybody, will not recognize the former.

You should certainly return -1, not (char)-1.

And what's the problem with returning -1 forever? Why limit the number of times you return it? EOF is EOF however many times it is requested. It would be more to the point to fix the code that keeps reading once it's already seen EOF. 49,999 reads after EOF is slightly excessive.

ejpa at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 5
You guys aren't understanding the purpose of the class. I want to redirect System.in.to a applet or a swing app. So, I want the Reader to accept the input, but not close the stream permenently. I hope that clear things up for a bit.
atoxica at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 6

Everything you've been told here is correct. We can't understand the purpose until you state it.

Now that you have, it doesn't clear anything up at all. Why all this elaboration? You can't close System.in anyway - all attempts are ignored. Why do you need System.setIn() if you just want to redirect the standard input to the applet?

Don't you just want to read the standard input as it is and display it? Have you tried it the simple way?

ejpa at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 7

> You guys aren't understanding the purpose of the

> class. I want to redirect System.in.to a applet or a

> swing app. So, I want the Reader to accept the

> input, but not close the stream permenently. I hope

> that clear things up for a bit.

Well, I had the same problem with my application, it would write the data fine, but not close the FileOutputStream, which caused my application to crash (because I put in safeguards) What I did was create variables for each thing being written, wrote a class, like:

public void Create() //coded 4/12/2006

{

create = new FileDialog(dialog,"New Game v1.0", FileDialog.SAVE);

create.show();

file_name = create.getFile();

f = new File(file_name);

if (file_name != null)

{ try

{

FileOutputStream fos = new FileOutputStream(file_name);

DataOutputStream dos = new DataOutputStream(fos);

level = "1";

cur_HP = "10";

max_HP = "10";

atk_str = "10";

def_str = "10";

cur_exp = "0";

exp_ned = "100";

loc_x = "0";

loc_y = "0";

dos.writeUTF(level);

dos.writeUTF(cur_HP);

dos.writeUTF(max_HP);

dos.writeUTF(atk_str);

dos.writeUTF(def_str);

dos.writeUTF(cur_exp);

dos.writeUTF(exp_ned);

dos.writeUTF(loc_x);

dos.writeUTF(loc_y);

System.out.println("File Created Sucessfully: " + file_name);

fos.close();

}

catch (IOException e)

{

System.out.println("There was an Error Creating the File: " + file_name);

System.out.println(e);

}

}

}

which opened the FileOutputStream and DataOutputStream, wrote the variables to the file (which were ints converted to a String), and closed the file. What I would try to do is to write your variables to the file, then put a end variable (like an int set to the value of 1728274672 or some other number of your choice), and when your reading your file, have an if/else statement like:

if(temp_process[i] != 1728274672)

{

process[i] = temp_process[i];

i = i + 1;

}

else

{

bufferedvariable.close();

}

i hope this makes sense

Message was edited by:

g@m3r

g@m3ra at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...
# 8
*gasp* Brilliant!I would put my input into a file, and set that file as the default input...That should work!Hmms...That would explain why the Reader works fine in the console and dies in my program. The console has its own buffers...
atoxica at 2007-7-7 16:57:42 > top of Java-index,Java Essentials,Java Programming...