Skip to content

Commit

Permalink
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -13,15 +13,19 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;

import jnr.constants.platform.Fcntl;

import org.jruby.RubyEncoding;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.util.ByteList;
import org.jruby.util.Dir;

import java.nio.ByteBuffer;
import java.util.Arrays;

public abstract class IOPrimitiveNodes {
@@ -204,23 +208,27 @@ public int write(VirtualFrame frame, RubyBasicObject file, RubyString string) {

// We have to copy here as write starts at byte[0], and the ByteList may not

byte[] bytes = string.getByteList().bytes();
final ByteList byteList = string.getByteList();

final ByteBuffer buffer = ByteBuffer.wrap(byteList.bytes(), byteList.begin(), byteList.length());

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum May 11, 2015

Contributor

Unfortunately, this still copies a buffer. You want to use ByteList#unsafeBytes instead.

This comment has been minimized.

Copy link
@eregon

eregon May 11, 2015

Author Member

Oh, right, thanks!


int total = 0;

while (bytes.length > 0) {
while (buffer.hasRemaining()) {
getContext().getSafepointManager().poll(this);

int written = posix().write(fd, bytes, bytes.length);
int written = posix().write(fd, buffer, buffer.remaining());

if (written == -1) {
throw new UnsupportedOperationException();
}

// Have to copy here again for the same reason!
buffer.position(buffer.position() + written);

bytes = Arrays.copyOfRange(bytes, written, bytes.length);
total += written;
}

return bytes.length;
return total;
}

}

0 comments on commit 6e6f23f

Please sign in to comment.