Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39885 > unrolled thread
| Started by | mike <mikaelpetterson@hotmail.com> |
|---|---|
| First post | 2023-01-02 02:10 -0800 |
| Last post | 2023-01-03 04:07 -0800 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Is it possible to send message bursts? mike <mikaelpetterson@hotmail.com> - 2023-01-02 02:10 -0800
Re: Is it possible to send message bursts? Arne Vajhøj <arne@vajhoej.dk> - 2023-01-02 10:42 -0500
Re: Is it possible to send message bursts? Andreas Leitgeb <avl@logic.at> - 2023-01-02 19:05 +0000
Re: Is it possible to send message bursts? Andreas Leitgeb <avl@logic.at> - 2023-01-02 19:13 +0000
Re: Is it possible to send message bursts? Arne Vajhøj <arne@vajhoej.dk> - 2023-01-02 19:27 -0500
Re: Is it possible to send message bursts? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-01-03 04:07 -0800
| From | mike <mikaelpetterson@hotmail.com> |
|---|---|
| Date | 2023-01-02 02:10 -0800 |
| Subject | Is it possible to send message bursts? |
| Message-ID | <7128506e-015c-4992-8ee4-9c62fb2ccc11n@googlegroups.com> |
Hi,
I am reading data from an inputstream. To test the function
I use a String that I convert to a byte array and the to a Stream.
using the following:
return new ByteArrayInputStream(message.getBytes());
I am reading in a loop like this:
int character = 1;
try (BufferedInputStream bis = new BufferedInputStream(inputStream)) {
while (running) {
if (!suspended)
character = bis.read();
Problem is when I test this is that I want to send message that first has some data and then no data giving making read() return -1 and then data again. Is it possible to create an inputstream that supports this behaviour?
br,
//mike
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-01-02 10:42 -0500 |
| Message-ID | <touu1b$9tp$1@gioia.aioe.org> |
| In reply to | #39885 |
On 1/2/2023 5:10 AM, mike wrote:
> I am reading data from an inputstream. To test the function
> I use a String that I convert to a byte array and the to a Stream.
> using the following:
> return new ByteArrayInputStream(message.getBytes());
>
> I am reading in a loop like this:
>
> int character = 1;
> try (BufferedInputStream bis = new BufferedInputStream(inputStream)) {
> while (running) {
> if (!suspended)
> character = bis.read();
>
>
> Problem is when I test this is that I want to send message that first
> has some data and then no data giving making read() return -1 and
> then data again. Is it possible to create an inputstream that
> supports this behaviour?
I don't understand. read() is a blocking method, so if there
is no input it blocks until there is input. It returns -1
if end of stream is reached (and stream is then invalid and
no point in calling read again).
You can easily write am InputStream class that pause a bit before
returning from read(), but does that really make sense?
Arne
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Date | 2023-01-02 19:05 +0000 |
| Message-ID | <slrntr6aod.pa7s.avl@logic.at> |
| In reply to | #39886 |
Arne Vajhøj <arne@vajhoej.dk> wrote: > On 1/2/2023 5:10 AM, mike wrote: >> Problem is when I test this is that I want to send message that first >> has some data and then no data giving making read() return -1 and >> then data again. Is it possible to create an inputstream that >> supports this behaviour? I'd think it is possible to create a custom InputStream class that allows reading some bytes, then -1 and lateron some more bytes, if its read() actually gets called again. The problem is, that standard-classes like BufferedReaders, or really any Reader, will just not expect further bytes after first EOF. Those wrappers will just assume that an EOF on the stream is final, and not even attempt any further read. So, if the code (that you want to pass your special stream to) relies on wrappers, it just won't work, but if it directly uses that InputStream and "knows" that -1 isn't final, it might even work - despite being fragile at best, as any software implementing undocumented/contradocumented behaviour. > I don't understand. read() is a blocking method, so if there > is no input it blocks until there is input. It returns -1 > if end of stream is reached (and stream is then invalid and > no point in calling read again). Outside of Java world, if stdin is bound to a terminal, then receiving an EOF and still being able to read more data till next EOF is not quite as bizarre as it might look like to Java'ists ;-)
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Date | 2023-01-02 19:13 +0000 |
| Message-ID | <slrntr6b74.pa7s.avl@logic.at> |
| In reply to | #39887 |
Andreas Leitgeb <avl@logic.at> wrote: > Arne Vajhøj <arne@vajhoej.dk> wrote: >> I don't understand. read() is a blocking method, so if there >> is no input it blocks until there is input. It returns -1 >> if end of stream is reached (and stream is then invalid and >> no point in calling read again). > > Outside of Java world, if stdin is bound to a terminal, then > receiving an EOF and still being able to read more data till > next EOF is not quite as bizarre as it might look like to > Java'ists ;-) Another use could be file-based Streams: You read a file to its end. Some other process appends data to the file. You could now (theoretically) read some more data. I must admit, I'm not sure(*), if that would work in Java, with its rather simple stream-abstraction, but it surely does seem reasonable in principle. *: and I'm too lazy to check ;-)
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-01-02 19:27 -0500 |
| Message-ID | <tovspv$10ol$1@gioia.aioe.org> |
| In reply to | #39887 |
On 1/2/2023 2:05 PM, Andreas Leitgeb wrote: > Arne Vajhøj <arne@vajhoej.dk> wrote: >> On 1/2/2023 5:10 AM, mike wrote: >>> Problem is when I test this is that I want to send message that first >>> has some data and then no data giving making read() return -1 and >>> then data again. Is it possible to create an inputstream that >>> supports this behaviour? > > I'd think it is possible to create a custom InputStream class > that allows reading some bytes, then -1 and lateron some more > bytes, if its read() actually gets called again. > > The problem is, that standard-classes like BufferedReaders, or > really any Reader, will just not expect further bytes after > first EOF. Those wrappers will just assume that an EOF on > the stream is final, and not even attempt any further read. > > So, if the code (that you want to pass your special stream to) > relies on wrappers, it just won't work, but if it directly uses > that InputStream and "knows" that -1 isn't final, it might > even work - despite being fragile at best, as any software > implementing undocumented/contradocumented behaviour. True. But if it was me then I would go in another direction. >> I don't understand. read() is a blocking method, so if there >> is no input it blocks until there is input. It returns -1 >> if end of stream is reached (and stream is then invalid and >> no point in calling read again). > > Outside of Java world, if stdin is bound to a terminal, then > receiving an EOF and still being able to read more data till > next EOF is not quite as bizarre as it might look like to > Java'ists ;-) True. But then that is not really EOF just CTRL/D or CTRL/Z. being treated like EOF. Arne
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-01-03 04:07 -0800 |
| Message-ID | <e01b6635-c20a-49bb-b3af-ceadac075620n@googlegroups.com> |
| In reply to | #39885 |
On Monday, January 2, 2023 at 5:10:45 AM UTC-5, mike wrote:
> Hi,
>
> I am reading data from an inputstream. To test the function
> I use a String that I convert to a byte array and the to a Stream.
> using the following:
> return new ByteArrayInputStream(message.getBytes());
>
> I am reading in a loop like this:
>
> int character = 1;
> try (BufferedInputStream bis = new BufferedInputStream(inputStream)) {
> while (running) {
> if (!suspended)
> character = bis.read();
>
>
> Problem is when I test this is that I want to send message that first has some data and then no data giving making read() return -1 and then data again. Is it possible to create an inputstream that supports this behaviour?
>
> br,
>
> //mike
Why would you need an inputstream to do that? What are you trying to connect? Maybe look at sockets?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web