forked from networknt/light-4j
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fe5eec
commit d5dcd45
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
98 changes: 98 additions & 0 deletions
98
client/src/main/java/com/networknt/client/listener/ByteBufferWriteChannelListener.java
This file contains 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,98 @@ | ||
package com.networknt.client.listener; | ||
|
||
import io.undertow.UndertowLogger; | ||
import org.xnio.ChannelListener; | ||
import org.xnio.ChannelListeners; | ||
import org.xnio.IoUtils; | ||
import org.xnio.channels.StreamSinkChannel; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* This is a new Channel Listener to handler Byte Array write from request. | ||
* | ||
* @author Gavin Chen | ||
*/ | ||
|
||
public class ByteBufferWriteChannelListener implements ChannelListener<StreamSinkChannel>{ | ||
|
||
private final ByteBuffer buffer; | ||
|
||
public ByteBufferWriteChannelListener(ByteBuffer body) { | ||
this.buffer =body; | ||
} | ||
|
||
public void setup(StreamSinkChannel channel) { | ||
while(true) { | ||
try { | ||
int c = channel.write(this.buffer); | ||
if (this.buffer.hasRemaining() && c > 0) { | ||
continue; | ||
} | ||
|
||
if (this.buffer.hasRemaining()) { | ||
channel.getWriteSetter().set(this); | ||
channel.resumeWrites(); | ||
} else { | ||
this.writeDone(channel); | ||
} | ||
} catch (IOException var3) { | ||
this.handleError(channel, var3); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
protected void handleError(StreamSinkChannel channel, IOException e) { | ||
UndertowLogger.REQUEST_IO_LOGGER.ioException(e); | ||
IoUtils.safeClose(channel); | ||
} | ||
|
||
public void handleEvent(StreamSinkChannel channel) { | ||
while(true) { | ||
try { | ||
int c = channel.write(this.buffer); | ||
if (this.buffer.hasRemaining() && c > 0) { | ||
continue; | ||
} | ||
|
||
if (this.buffer.hasRemaining()) { | ||
channel.resumeWrites(); | ||
return; | ||
} | ||
|
||
this.writeDone(channel); | ||
} catch (IOException var3) { | ||
this.handleError(channel, var3); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
public boolean hasRemaining() { | ||
return this.buffer.hasRemaining(); | ||
} | ||
|
||
protected void writeDone(final StreamSinkChannel channel) { | ||
try { | ||
channel.shutdownWrites(); | ||
if (!channel.flush()) { | ||
channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() { | ||
public void handleEvent(StreamSinkChannel o) { | ||
IoUtils.safeClose(channel); | ||
} | ||
}, ChannelListeners.closingChannelExceptionHandler())); | ||
channel.resumeWrites(); | ||
} | ||
} catch (IOException var3) { | ||
this.handleError(channel, var3); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|