protected void write(MessageWriter writer) throws IOException {
CancellableDataOutputStream output = new CancellableDataOutputStream(channel.writeMessage());
try {
writer.write(output);
} catch (IOException e) {
output.cancel();
throw e;
} finally {
IoUtils.safeClose(output);
}
}