Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created December 31, 2012 20:22
Show Gist options
  • Save darkfrog26/4422464 to your computer and use it in GitHub Desktop.
Save darkfrog26/4422464 to your computer and use it in GitHub Desktop.
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