| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
package com.sun.grizzly.util; |
| 40 | |
|
| 41 | |
import java.io.IOException; |
| 42 | |
import java.net.SocketAddress; |
| 43 | |
import java.nio.ByteBuffer; |
| 44 | |
import java.nio.channels.DatagramChannel; |
| 45 | |
import java.nio.channels.SelectableChannel; |
| 46 | |
import java.nio.channels.SelectionKey; |
| 47 | |
import java.nio.channels.Selector; |
| 48 | |
import java.nio.channels.SocketChannel; |
| 49 | |
import java.nio.channels.WritableByteChannel; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | public class OutputWriter { |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 1 | private static int defaultWriteTimeout = 30000; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public static long flushChannel(SelectableChannel channel, ByteBuffer bb) |
| 72 | |
throws IOException{ |
| 73 | 4093 | return flushChannel(channel,bb,defaultWriteTimeout); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public static long flushChannel(SelectableChannel channel, |
| 86 | |
ByteBuffer bb, long writeTimeout) throws IOException{ |
| 87 | |
|
| 88 | 4093 | if (bb == null){ |
| 89 | 0 | throw new IllegalStateException("Invalid Response State. ByteBuffer" |
| 90 | |
+ " cannot be null."); |
| 91 | |
} |
| 92 | |
|
| 93 | 4093 | if (channel == null){ |
| 94 | 0 | throw new IllegalStateException("Invalid Response State. " + |
| 95 | |
"SocketChannel cannot be null."); |
| 96 | |
} |
| 97 | |
|
| 98 | 4093 | SelectionKey key = null; |
| 99 | 4093 | Selector writeSelector = null; |
| 100 | 4093 | int attempts = 0; |
| 101 | 4093 | int bytesProduced = 0; |
| 102 | |
try { |
| 103 | 4093 | WritableByteChannel writableChannel = (WritableByteChannel) channel; |
| 104 | 8185 | while ( bb.hasRemaining() ) { |
| 105 | 4093 | int len = writableChannel.write(bb); |
| 106 | 4092 | if (len > 0){ |
| 107 | 4092 | attempts = 0; |
| 108 | 4092 | bytesProduced += len; |
| 109 | |
} else { |
| 110 | 0 | attempts++; |
| 111 | 0 | if ( writeSelector == null ){ |
| 112 | 0 | writeSelector = SelectorFactory.getSelector(); |
| 113 | 0 | if ( writeSelector == null){ |
| 114 | |
|
| 115 | 0 | continue; |
| 116 | |
} |
| 117 | 0 | key = channel.register(writeSelector, |
| 118 | |
SelectionKey.OP_WRITE); |
| 119 | |
} |
| 120 | |
|
| 121 | 0 | if (writeSelector.select(writeTimeout) == 0) { |
| 122 | 0 | if (attempts > 2) |
| 123 | 0 | throw new IOException("Client disconnected"); |
| 124 | |
} |
| 125 | |
} |
| 126 | 4092 | } |
| 127 | |
} finally { |
| 128 | 4093 | if (key != null) { |
| 129 | 0 | key.cancel(); |
| 130 | 0 | key = null; |
| 131 | |
} |
| 132 | |
|
| 133 | 4093 | if ( writeSelector != null ) { |
| 134 | |
|
| 135 | 0 | SelectorFactory.selectNowAndReturnSelector(writeSelector); |
| 136 | |
} |
| 137 | |
} |
| 138 | 4092 | return bytesProduced; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public static long flushChannel(SocketChannel socketChannel, ByteBuffer[] bb) |
| 150 | |
throws IOException{ |
| 151 | 0 | return flushChannel(socketChannel,bb,defaultWriteTimeout); |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public static long flushChannel(SocketChannel socketChannel, |
| 164 | |
ByteBuffer[] bb, long writeTimeout) throws IOException{ |
| 165 | |
|
| 166 | 0 | if (bb == null){ |
| 167 | 0 | throw new IllegalStateException("Invalid Response State. ByteBuffer" |
| 168 | |
+ " cannot be null."); |
| 169 | |
} |
| 170 | |
|
| 171 | 0 | if (socketChannel == null){ |
| 172 | 0 | throw new IllegalStateException("Invalid Response State. " + |
| 173 | |
"SocketChannel cannot be null."); |
| 174 | |
} |
| 175 | |
|
| 176 | 0 | SelectionKey key = null; |
| 177 | 0 | Selector writeSelector = null; |
| 178 | 0 | int attempts = 0; |
| 179 | 0 | long totalBytes = 0; |
| 180 | 0 | for (ByteBuffer aBb : bb) { |
| 181 | 0 | totalBytes += aBb.remaining(); |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | long bytesProduced = 0; |
| 185 | |
try { |
| 186 | 0 | while (bytesProduced < totalBytes ) { |
| 187 | 0 | long len = socketChannel.write(bb); |
| 188 | 0 | if (len > 0){ |
| 189 | 0 | attempts = 0; |
| 190 | 0 | bytesProduced += len; |
| 191 | |
} else { |
| 192 | 0 | if ( writeSelector == null ){ |
| 193 | 0 | writeSelector = SelectorFactory.getSelector(); |
| 194 | 0 | if ( writeSelector == null){ |
| 195 | |
|
| 196 | 0 | continue; |
| 197 | |
} |
| 198 | |
} |
| 199 | |
|
| 200 | 0 | key = socketChannel.register(writeSelector, |
| 201 | |
SelectionKey.OP_WRITE); |
| 202 | |
|
| 203 | 0 | if (writeSelector.select(writeTimeout) == 0) { |
| 204 | 0 | if (attempts > 2) |
| 205 | 0 | throw new IOException("Client disconnected"); |
| 206 | |
} |
| 207 | |
} |
| 208 | 0 | } |
| 209 | |
} finally { |
| 210 | 0 | if (key != null) { |
| 211 | 0 | key.cancel(); |
| 212 | 0 | key = null; |
| 213 | |
} |
| 214 | |
|
| 215 | 0 | if ( writeSelector != null ) { |
| 216 | |
|
| 217 | 0 | SelectorFactory.selectNowAndReturnSelector(writeSelector); |
| 218 | |
} |
| 219 | |
} |
| 220 | 0 | return bytesProduced; |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
public static long flushChannel(DatagramChannel datagramChannel, |
| 233 | |
SocketAddress socketAddress, ByteBuffer bb) |
| 234 | |
throws IOException{ |
| 235 | 1002 | return flushChannel(datagramChannel,socketAddress,bb,defaultWriteTimeout); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public static long flushChannel(DatagramChannel datagramChannel, |
| 249 | |
SocketAddress socketAddress, ByteBuffer bb, long writeTimeout) |
| 250 | |
throws IOException{ |
| 251 | |
|
| 252 | 1002 | if (bb == null){ |
| 253 | 0 | throw new IllegalStateException("Invalid Response State. ByteBuffer" |
| 254 | |
+ " cannot be null."); |
| 255 | |
} |
| 256 | |
|
| 257 | 1002 | if (datagramChannel == null){ |
| 258 | 0 | throw new IllegalStateException("Invalid Response State. " + |
| 259 | |
"DatagramChannel cannot be null."); |
| 260 | |
} |
| 261 | |
|
| 262 | 1002 | if (socketAddress == null){ |
| 263 | 0 | throw new IllegalStateException("Invalid Response State. " + |
| 264 | |
"SocketAddress cannot be null."); |
| 265 | |
} |
| 266 | |
|
| 267 | 1002 | SelectionKey key = null; |
| 268 | 1002 | Selector writeSelector = null; |
| 269 | 1002 | int attempts = 0; |
| 270 | 1002 | int bytesProduced = 0; |
| 271 | |
try { |
| 272 | 2004 | while ( bb.hasRemaining() ) { |
| 273 | 1002 | int len = datagramChannel.send(bb,socketAddress); |
| 274 | 1002 | if (len > 0){ |
| 275 | 1002 | attempts = 0; |
| 276 | 1002 | bytesProduced += len; |
| 277 | |
} else { |
| 278 | 0 | if ( writeSelector == null ){ |
| 279 | 0 | writeSelector = SelectorFactory.getSelector(); |
| 280 | 0 | if ( writeSelector == null){ |
| 281 | |
|
| 282 | 0 | continue; |
| 283 | |
} |
| 284 | |
} |
| 285 | |
|
| 286 | 0 | key = datagramChannel.register(writeSelector, |
| 287 | |
SelectionKey.OP_WRITE); |
| 288 | |
|
| 289 | 0 | if (writeSelector.select(writeTimeout) == 0) { |
| 290 | 0 | if (attempts > 2) |
| 291 | 0 | throw new IOException("Client disconnected"); |
| 292 | |
} else { |
| 293 | 0 | attempts--; |
| 294 | |
} |
| 295 | |
} |
| 296 | 1002 | } |
| 297 | |
} finally { |
| 298 | 1002 | if (key != null) { |
| 299 | 0 | key.cancel(); |
| 300 | 0 | key = null; |
| 301 | |
} |
| 302 | |
|
| 303 | 1002 | if ( writeSelector != null ) { |
| 304 | |
|
| 305 | 0 | SelectorFactory.selectNowAndReturnSelector(writeSelector); |
| 306 | |
} |
| 307 | |
} |
| 308 | 1002 | return bytesProduced; |
| 309 | |
} |
| 310 | |
|
| 311 | |
|
| 312 | |
public static int getDefaultWriteTimeout() { |
| 313 | 0 | return defaultWriteTimeout; |
| 314 | |
} |
| 315 | |
|
| 316 | |
|
| 317 | |
public static void setDefaultWriteTimeout(int aDefaultWriteTimeout) { |
| 318 | 0 | defaultWriteTimeout = aDefaultWriteTimeout; |
| 319 | 0 | } |
| 320 | |
} |