> Daniel
>
[quoted text clipped - 9 lines]
> I hope this helps.
> Dustin
Did you even read my message?
Give us code that recreates your problem. It doesn't have to be the
exact same code as your proprietary code, but it should suffer the same
issues.
Also, is broken code worth being proprietary? :-)
The following works for me, I'm guessing it is some other problem with
your code.
<sscce>
import java.io.*;
public class FileOutputStreamTest {
public static void main(String...args) throws IOException {
File file = new
File("net.virtualinfinity.FileOutputStreamText.dat");
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file,
true);
fileOutputStream.write(new byte[] {1, 2, 3, 4, 5});
fileOutputStream.flush();
fileOutputStream.write(new byte[] {6, 7, 8, 9, 10});
fileOutputStream.flush();
fileOutputStream.close();
FileInputStream inputStream = new FileInputStream(file);
byte[] input = new byte[inputStream.available()];
inputStream.read(input);
if (input.length != 10) {
System.out.println("Expected 10 bytes, but got " +
input.length);
} else {
for (int i = 0; i < 10; ++i) {
if (input[i] != i + 1) {
System.out.println("Data is incorrect at location "
+ i);
}
}
}
}
}
</sscce>
Chris Uppal - 04 Jan 2007 19:13 GMT
> byte[] input = new byte[inputStream.available()];
> inputStream.read(input);
You shouldn't ever do that, especially not in example code posted where
beginners will see it. If you ignore the return value from read() then your
code is broken.
(Apart from that side note, I agree with you -- there is nothing obviously
wrong with the OP's approach so the problem must be somewhere in the code he
declines to show us).
-- chris
Daniel Pitts - 04 Jan 2007 19:20 GMT
> > byte[] input = new byte[inputStream.available()];
> > inputStream.read(input);
[quoted text clipped - 8 lines]
>
> -- chris
Thanks for the note. I don't do much IO in java, so I'm not used to
all the caveats.
Mark Thornton - 04 Jan 2007 20:14 GMT
>> byte[] input = new byte[inputStream.available()];
>> inputStream.read(input);
[quoted text clipped - 6 lines]
> wrong with the OP's approach so the problem must be somewhere in the code he
> declines to show us).
The other problem is the definition of available():
"Returns an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking by the next invocation of
a method for this input stream. The next invocation might be the same
thread or another thread. A single read or skip of this many bytes will
not block, but may read or skip fewer bytes.
Note that while some implementations of InputStream will return the
total number of bytes in the stream, many will not. It is never correct
to use the return value of this method to allocate a buffer intended to
hold all data in this stream"
Note particularly the last sentence.
Mark Thornton
> Daniel
>
[quoted text clipped - 9 lines]
> I hope this helps.
> Dustin
Unfortunately, "basically all I am doing" does not usually work. The
details that seem insignificant, or are even totally invisible, to the
programmer are exactly the details where bugs lurk.
Producing a self-contained example is an important debug step, even if
you are not using a newsgroup. If the problem really is where you think
it is, you should be able to work up your code snippets into a running,
but failing, example program in a few minutes. If it isn't, you need to
know that.
It is quite likely that you will find the bug yourself in the course of
producing the example. However, once you have a short failing program
you can always edit identifiers to disguise what you are really doing
and post the result.
Patricia