-
Notifications
You must be signed in to change notification settings - Fork 5
(new) VertxInputStream #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trajano
wants to merge
4
commits into
master
Choose a base branch
from
remove-blocking-input-stream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
ms-engine/src/main/java/net/trajano/ms/engine/internal/VertxInputStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package net.trajano.ms.engine.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.Semaphore; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.streams.ReadStream; | ||
|
||
/** | ||
* <p> | ||
* </p> | ||
* An input stream that wraps a Vert.X Read Buffer. This does not do anything | ||
* that performs blocks. | ||
* </p> | ||
* <p> | ||
* This pauses() the stream once the buffer is read and resumes the stream when | ||
* the buffer is fully read. | ||
* </p> | ||
*/ | ||
public class VertxInputStream extends InputStream { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(VertxInputStream.class); | ||
|
||
/** | ||
* Bytes read counter. | ||
*/ | ||
private long bytesRead = 0; | ||
|
||
/** | ||
* Flag to indicate that the InputStream is closed. | ||
*/ | ||
private boolean closed = false; | ||
|
||
/** | ||
* Current buffer. | ||
*/ | ||
private volatile Buffer currentBuffer; | ||
|
||
/** | ||
* Flag to indicate that the ReadStream is ended. | ||
*/ | ||
private boolean ended = false; | ||
|
||
/** | ||
* Exception holder. If this is not null, it will be thrown on the next read. | ||
*/ | ||
private IOException exceptionToThrow = null; | ||
|
||
/** | ||
* Current position in buffer. | ||
*/ | ||
private volatile int pos; | ||
|
||
/** | ||
* Read Stream being wrapped. | ||
*/ | ||
private final ReadStream<Buffer> readStream; | ||
|
||
/** | ||
* Semaphore to lock. Permits is equivalent to available bytes. | ||
*/ | ||
private final Semaphore streamSemaphore = new Semaphore(0); | ||
|
||
public VertxInputStream(final ReadStream<Buffer> readStream) { | ||
|
||
this.readStream = readStream; | ||
readStream.pause(); | ||
readStream | ||
.handler(this::populate) | ||
.exceptionHandler(this::error) | ||
.endHandler(aVoid -> end()); | ||
|
||
} | ||
|
||
@Override | ||
public int available() throws IOException { | ||
|
||
return currentBuffer == null ? 0 : currentBuffer.length() - pos; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
|
||
closed = true; | ||
|
||
} | ||
|
||
/** | ||
* Signals that the readstream has ended. | ||
*/ | ||
public void end() { | ||
|
||
System.out.println("ended"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
ended = true; | ||
|
||
} | ||
|
||
/** | ||
* End the buffer because of an error. | ||
* | ||
* @param e | ||
*/ | ||
public void error(final Throwable e) { | ||
|
||
exceptionToThrow = new IOException(e); | ||
|
||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Sets the buffer with a new one from the stream then pauses. | ||
* | ||
* @param buffer | ||
* buffer from stream. | ||
*/ | ||
public void populate(final Buffer buffer) { | ||
|
||
System.out.println("populating..." + currentBuffer != null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (currentBuffer != null) { | ||
readStream.pause(); | ||
} else { | ||
if (streamSemaphore.availablePermits() > 1) { | ||
throw new RuntimeException("p=" + streamSemaphore.availablePermits()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
currentBuffer = buffer; | ||
pos = 0; | ||
streamSemaphore.release(); | ||
} | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
|
||
if (exceptionToThrow != null) { | ||
throw exceptionToThrow; | ||
} | ||
|
||
if (closed) { | ||
throw new IOException("Stream is closed"); | ||
} | ||
if (available() == 0) { | ||
if (ended) { | ||
return -1; | ||
} | ||
currentBuffer = null; | ||
readStream.resume(); | ||
streamSemaphore.acquireUninterruptibly(); | ||
} | ||
if (ended && available() == 0) { | ||
return -1; | ||
} | ||
// Convert to unsigned byte | ||
final int b = currentBuffer.getByte(pos++) & 0xFF; | ||
++bytesRead; | ||
|
||
return b; | ||
|
||
} | ||
|
||
@Override | ||
public void reset() throws IOException { | ||
|
||
throw new IOException("reset not supported"); | ||
} | ||
|
||
/** | ||
* Gets a count of how much bytes have been read from this input stream. | ||
* | ||
* @return total bytes read | ||
*/ | ||
public long totalBytesRead() { | ||
|
||
return bytesRead; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.