001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt.nio;
009    
010    import java.io.IOException;
011    import java.io.OutputStream;
012    import java.nio.ByteBuffer;
013    
014    /**
015     * {@link OutputStream} for UDT sockets.
016     */
017    public class NioOutputStreamUDT extends OutputStream {
018    
019            protected final SocketChannelUDT channel;
020    
021            /**
022             * Creates a new UDT output stream.
023             * 
024             * @param channel
025             *            The UDT socket channel.
026             */
027            protected NioOutputStreamUDT(final SocketChannelUDT channel) {
028                    this.channel = channel;
029            }
030    
031            @Override
032            public void write(final byte[] bytes, final int off, final int len)
033                            throws IOException {
034                    channel.write(ByteBuffer.wrap(bytes, off, len));
035            }
036    
037            @Override
038            public void write(final byte[] bytes) throws IOException {
039                    channel.write(ByteBuffer.wrap(bytes));
040            }
041    
042            @Override
043            public void write(final int b) throws IOException {
044                    // Just cast it -- this is the same thing SocketOutputStream does.
045                    final byte[] bytes = { (byte) b };
046                    channel.write(ByteBuffer.wrap(bytes));
047            }
048    
049            @Override
050            public void close() throws IOException {
051                    channel.close();
052            }
053    
054    }