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.net;
009    
010    import java.io.IOException;
011    import java.io.OutputStream;
012    import java.nio.channels.IllegalBlockingModeException;
013    
014    import com.barchart.udt.ErrorUDT;
015    import com.barchart.udt.SocketUDT;
016    
017    /**
018     * {@link OutputStream} for UDT sockets.
019     */
020    public class NetOutputStreamUDT extends OutputStream {
021    
022            protected final SocketUDT socketUDT;
023    
024            /**
025             * 
026             * @param socketUDT
027             *            The UDT socket.
028             */
029            public NetOutputStreamUDT(final SocketUDT socketUDT) {
030    
031                    if (!socketUDT.isBlocking()) {
032                            throw new IllegalBlockingModeException();
033                    }
034    
035                    this.socketUDT = socketUDT;
036    
037            }
038    
039            @Override
040            public void write(final int b) throws IOException {
041    
042                    // Just cast it -- this is the same thing SocketOutputStream does.
043                    final byte[] bytes = { (byte) b };
044    
045                    write(bytes);
046    
047            }
048    
049            @Override
050            public void write(final byte[] bytes) throws IOException {
051    
052                    write(bytes, 0, bytes.length);
053    
054            }
055    
056            @Override
057            public void write(final byte[] bytes, final int off, final int len)
058                            throws IOException {
059    
060                    int bytesRemaining = len;
061    
062                    while (bytesRemaining > 0) {
063    
064                            final int count = socketUDT.send(bytes, off + len - bytesRemaining,
065                                            off + len);
066    
067                            if (count > 0) {
068                                    bytesRemaining -= count;
069                                    continue;
070                            }
071    
072                            if (count == 0) {
073                                    throw new ExceptionSendUDT(socketUDT.id(),
074                                                    ErrorUDT.USER_DEFINED_MESSAGE, "UDT send time out");
075                            }
076    
077                            throw new IllegalStateException(
078                                            "Socket has been chaged to non-blocking");
079                    }
080    
081            }
082    
083            @Override
084            public void close() throws IOException {
085                    socketUDT.close();
086            }
087    
088    }