public
Created

  • Download Gist
gistfile1.diff
Diff
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
diff --git a/src/org/jruby/Ruby.java b/src/org/jruby/Ruby.java
index 0a17484..e7239da 100644
--- a/src/org/jruby/Ruby.java
+++ b/src/org/jruby/Ruby.java
@@ -3189,100 +3189,104 @@ public final class Ruby {
}
public RaiseException newErrnoENOTDIRError(String message) {
return newRaiseException(getErrno().getClass("ENOTDIR"), message);
}
public RaiseException newErrnoENOTEMPTYError(String message) {
return newRaiseException(getErrno().getClass("ENOTEMPTY"), message);
}
public RaiseException newErrnoENOTSOCKError(String message) {
return newRaiseException(getErrno().getClass("ENOTSOCK"), message);
}
public RaiseException newErrnoENOTCONNError(String message) {
return newRaiseException(getErrno().getClass("ENOTCONN"), message);
}
public RaiseException newErrnoENOTCONNError() {
return newRaiseException(getErrno().getClass("ENOTCONN"), "Socket is not connected");
}
public RaiseException newErrnoENOENTError(String message) {
return newRaiseException(getErrno().getClass("ENOENT"), message);
}
public RaiseException newErrnoESPIPEError(String message) {
return newRaiseException(getErrno().getClass("ESPIPE"), message);
}
public RaiseException newErrnoEEXISTError(String message) {
return newRaiseException(getErrno().getClass("EEXIST"), message);
}
public RaiseException newErrnoEDOMError(String message) {
return newRaiseException(getErrno().getClass("EDOM"), "Domain error - " + message);
}
public RaiseException newErrnoECHILDError() {
return newRaiseException(getErrno().getClass("ECHILD"), "No child processes");
}
public RaiseException newErrnoEADDRNOTAVAILError(String message) {
return newRaiseException(getErrno().getClass("EADDRNOTAVAIL"), message);
}
public RaiseException newErrnoESRCHError() {
return newRaiseException(getErrno().getClass("ESRCH"), null);
}
+ public RaiseException newErrnoEWOULDBLOCKError() {
+ return newRaiseException(getErrno().getClass("EWOULDBLOCK"), null);
+ }
+
public RaiseException newIndexError(String message) {
return newRaiseException(getIndexError(), message);
}
public RaiseException newSecurityError(String message) {
return newRaiseException(getSecurityError(), message);
}
public RaiseException newSystemCallError(String message) {
return newRaiseException(getSystemCallError(), message);
}
public RaiseException newKeyError(String message) {
return newRaiseException(getKeyError(), message);
}
public RaiseException newErrnoFromLastPOSIXErrno() {
return newRaiseException(getErrno(getPosix().errno()), null);
}
public RaiseException newErrnoFromInt(int errno, String message) {
RubyClass errnoClass = getErrno(errno);
if (errnoClass != null) {
return newRaiseException(errnoClass, message);
} else {
return newSystemCallError("Unknown Error (" + errno + ") - " + message);
}
}
public RaiseException newErrnoFromInt(int errno) {
Errno errnoObj = Errno.valueOf(errno);
if (errnoObj == null) {
return newSystemCallError("Unknown Error (" + errno + ")");
}
String message = errnoObj.description();
return newErrnoFromInt(errno, message);
}
private final static Pattern ADDR_NOT_AVAIL_PATTERN = Pattern.compile("assign.*address");
public RaiseException newErrnoEADDRFromBindException(BindException be) {
String msg = be.getMessage();
if (msg == null) {
msg = "bind";
} else {
msg = "bind - " + msg;
}
// This is ugly, but what can we do, Java provides the same BindingException
// for both EADDRNOTAVAIL and EADDRINUSE, so we differentiate the errors
// based on BindException's message.
diff --git a/src/org/jruby/RubyIO.java b/src/org/jruby/RubyIO.java
index 70bc75a..a24355a 100644
--- a/src/org/jruby/RubyIO.java
+++ b/src/org/jruby/RubyIO.java
@@ -1213,101 +1213,106 @@ public class RubyIO extends RubyObject {
OpenFile myOpenFile = getOpenFileChecked();
myOpenFile.checkWritable(runtime);
Stream writeStream = myOpenFile.getWriteStream();
if (myOpenFile.isWriteBuffered()) {
runtime.getWarnings().warn(ID.SYSWRITE_BUFFERED_IO, "syswrite for buffered IO");
}
if (!writeStream.getDescriptor().isWritable()) {
myOpenFile.checkClosed(runtime);
}
context.getThread().beforeBlockingCall();
int read = writeStream.getDescriptor().write(string.getByteList());
if (read == -1) {
// TODO? I think this ends up propagating from normal Java exceptions
// sys_fail(openFile.getPath())
}
return runtime.newFixnum(read);
} catch (InvalidValueException ex) {
throw runtime.newErrnoEINVALError();
} catch (BadDescriptorException e) {
throw runtime.newErrnoEBADFError();
} catch (IOException e) {
throw runtime.newSystemCallError(e.getMessage());
} finally {
context.getThread().afterBlockingCall();
}
}
@JRubyMethod(name = "write_nonblock", required = 1)
public IRubyObject write_nonblock(ThreadContext context, IRubyObject obj) {
// MRI behavior: always check whether the file is writable
// or not, even if we are to write 0 bytes.
OpenFile myOpenFile = getOpenFileChecked();
try {
myOpenFile.checkWritable(context.getRuntime());
RubyString str = obj.asString();
if (str.getByteList().length() == 0) {
return context.getRuntime().newFixnum(0);
}
if (myOpenFile.isWriteBuffered()) {
context.getRuntime().getWarnings().warn(ID.SYSWRITE_BUFFERED_IO, "write_nonblock for buffered IO");
}
- int written = myOpenFile.getWriteStream().getDescriptor().write(str.getByteList());
+
+ ChannelStream stream = (ChannelStream)myOpenFile.getWriteStream();
+
+ int written = stream.writenonblock(str.getByteList());
+ if (written == 0) throw context.runtime.newErrnoEWOULDBLOCKError();
+
return context.getRuntime().newFixnum(written);
} catch (IOException ex) {
throw context.getRuntime().newIOErrorFromException(ex);
} catch (BadDescriptorException ex) {
throw context.getRuntime().newErrnoEBADFError();
} catch (InvalidValueException ex) {
throw context.getRuntime().newErrnoEINVALError();
}
}
/** io_write
*
*/
@JRubyMethod(name = "write", required = 1)
public IRubyObject write(ThreadContext context, IRubyObject obj) {
Ruby runtime = context.getRuntime();
runtime.secure(4);
RubyString str = obj.asString();
// TODO: Ruby reuses this logic for other "write" behavior by checking if it's an IO and calling write again
if (str.getByteList().length() == 0) {
return runtime.newFixnum(0);
}
try {
OpenFile myOpenFile = getOpenFileChecked();
myOpenFile.checkWritable(runtime);
context.getThread().beforeBlockingCall();
int written = fwrite(str.getByteList());
if (written == -1) {
// TODO: sys fail
}
// if not sync, we switch to write buffered mode
if (!myOpenFile.isSync()) {
myOpenFile.setWriteBuffered();
}
return runtime.newFixnum(written);
} catch (IOException ex) {
throw runtime.newIOErrorFromException(ex);
} catch (BadDescriptorException ex) {
throw runtime.newErrnoEBADFError();
} catch (InvalidValueException ex) {
diff --git a/src/org/jruby/util/io/ChannelStream.java b/src/org/jruby/util/io/ChannelStream.java
index 0ab9d90..c673625 100644
--- a/src/org/jruby/util/io/ChannelStream.java
+++ b/src/org/jruby/util/io/ChannelStream.java
@@ -1207,100 +1207,101 @@ public class ChannelStream implements Stream, Finalizable {
public synchronized int fgetc() throws IOException, BadDescriptorException {
if (eof) {
return -1;
}
checkReadable();
int c = read();
if (c == -1) {
eof = true;
return c;
}
return c & 0xff;
}
public synchronized int fwrite(ByteList string) throws IOException, BadDescriptorException {
return bufferedWrite(string);
}
public synchronized int write(ByteBuffer buf) throws IOException, BadDescriptorException {
return bufferedWrite(buf);
}
public synchronized int writenonblock(ByteList buf) throws IOException, BadDescriptorException {
checkWritable();
ensureWrite();
// Ruby ignores empty syswrites
if (buf == null || buf.length() == 0) return 0;
if (buffer.position() != 0 && !flushWrite(false)) return 0;
if (descriptor.getChannel() instanceof SelectableChannel) {
SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel();
synchronized (selectableChannel.blockingLock()) {
boolean oldBlocking = selectableChannel.isBlocking();
try {
if (oldBlocking) {
selectableChannel.configureBlocking(false);
}
return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
} finally {
if (oldBlocking) {
selectableChannel.configureBlocking(oldBlocking);
}
}
}
} else {
+ // can't set nonblocking, so go ahead with it...not much else we can do
return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
}
}
public synchronized ByteList fread(int number) throws IOException, BadDescriptorException {
try {
if (number == 0) {
if (eof) {
return null;
} else {
return new ByteList(0);
}
}
return bufferedRead(number);
} catch (EOFException e) {
eof = true;
return null;
}
}
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException {
assert number >= 0;
if (number == 0) {
return null;
}
if (descriptor.getChannel() instanceof SelectableChannel) {
SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel();
synchronized (selectableChannel.blockingLock()) {
boolean oldBlocking = selectableChannel.isBlocking();
try {
selectableChannel.configureBlocking(false);
return readpartial(number);
} finally {
selectableChannel.configureBlocking(oldBlocking);
}
}
} else if (descriptor.getChannel() instanceof FileChannel) {
return fread(number);
} else {
return null;
}
}
public synchronized ByteList readpartial(int number) throws IOException, BadDescriptorException, EOFException {
assert number >= 0;
if (number == 0) {

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.