Created
December 31, 2012 20:22
-
-
Save darkfrog26/4422464 to your computer and use it in GitHub Desktop.
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
import org.jboss.netty.channel.{ChannelFuture, Channel} | |
import org.jboss.netty.buffer.ChannelBuffers | |
import org.jboss.netty.util.CharsetUtil | |
import java.nio.charset.Charset | |
/** | |
* @author Matt Hicks <mhicks@outr.com> | |
*/ | |
class ChannelStringWriter(channel: Channel, buffer: Int = 512, charset: Charset = CharsetUtil.UTF_8) { | |
private val b = new StringBuilder | |
private var writeFuture: ChannelFuture = null | |
def write(s: String) = { | |
b.append(s) | |
if (b.length >= buffer) { | |
flush() | |
} | |
} | |
def flush() = { | |
if (b.length > 0) { | |
writeFuture = channel.write(ChannelBuffers.wrappedBuffer(b.toString().getBytes(charset))) | |
b.clear() | |
} | |
} | |
def finish() = { | |
flush() | |
writeFuture | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment