Skip to content

Commit

Permalink
cross compile compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek committed Nov 15, 2018
1 parent bc004e2 commit 68f4722
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 80 deletions.
32 changes: 16 additions & 16 deletions core/src/main/java/org/jruby/RubyEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static CharBuffer toCharBuffer(CharSequence str) {
}

private static byte[] getBytes(final ByteBuffer buffer) {
byte[] bytes = new byte[buffer.limit()];
byte[] bytes = new byte[((Buffer) buffer).limit()];
buffer.get(bytes);
return bytes;
}
Expand All @@ -223,7 +223,7 @@ private static ByteList getByteList(final ByteBuffer buffer, final Encoding enc,
bytes = getBytes(buffer);
off = 0;
}
return new ByteList(bytes, off, off + buffer.limit(), enc, false);
return new ByteList(bytes, off, off + ((Buffer) buffer).limit(), enc, false);
}

public static byte[] encodeUTF16(String str) {
Expand All @@ -244,7 +244,7 @@ static ByteList doEncodeUTF16(CharSequence str) {

public static byte[] encode(CharSequence cs, Charset charset) {
ByteBuffer buffer = charset.encode(CharBuffer.wrap(cs));
byte[] bytes = new byte[buffer.limit()];
byte[] bytes = new byte[((Buffer) buffer).limit()];
buffer.get(bytes);
return bytes;
}
Expand All @@ -259,7 +259,7 @@ static ByteList doEncode(CharSequence cs, Charset charset, Encoding enc) {

public static byte[] encode(String str, Charset charset) {
ByteBuffer buffer = charset.encode(str);
byte[] bytes = new byte[buffer.limit()];
byte[] bytes = new byte[((Buffer) buffer).limit()];
buffer.get(bytes);
return bytes;
}
Expand Down Expand Up @@ -303,40 +303,40 @@ private static class UTF8Coder {
public final ByteBuffer encode(String str) {
ByteBuffer buf = byteBuffer;
CharBuffer cbuf = charBuffer;
buf.clear();
cbuf.clear();
((Buffer) buf).clear();
((Buffer) cbuf).clear();
cbuf.put(str);
cbuf.flip();
((Buffer) cbuf).flip();
encoder.encode(cbuf, buf, true);
buf.flip();
((Buffer) buf).flip();

return buf;
}

public final ByteBuffer encode(CharSequence str) {
ByteBuffer buf = byteBuffer;
CharBuffer cbuf = charBuffer;
buf.clear();
cbuf.clear();
((Buffer) buf).clear();
((Buffer) cbuf).clear();
// NOTE: doesn't matter is we toString here in terms of speed
// ... so we "safe" some space at least by not copy-ing char[]
for (int i = 0; i < str.length(); i++) cbuf.put(str.charAt(i));
cbuf.flip();
((Buffer) cbuf).flip();
encoder.encode(cbuf, buf, true);
buf.flip();
((Buffer) buf).flip();

return buf;
}

public final CharBuffer decode(byte[] bytes, int start, int length) {
CharBuffer cbuf = charBuffer;
ByteBuffer buf = byteBuffer;
cbuf.clear();
buf.clear();
((Buffer) cbuf).clear();
((Buffer) buf).clear();
buf.put(bytes, start, length);
buf.flip();
((Buffer) buf).flip();
decoder.decode(buf, cbuf, true);
cbuf.flip();
((Buffer) cbuf).flip();

return cbuf;
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
Expand Down Expand Up @@ -4443,9 +4444,9 @@ private static long transfer(ThreadContext context, ReadableByteChannel from, Wr

if (n == -1) break;

buffer.flip();
((Buffer) buffer).flip();
to.write(buffer);
buffer.clear();
((Buffer) buffer).clear();

transferred += n;
if (length > 0) {
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/embed/io/ReaderInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -105,12 +106,12 @@ private void fillByteBuffer(Reader reader) throws IOException {
ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
List<byte[]> list = new ArrayList<byte[]>();
while (true) {
cbuf.clear();
((Buffer) cbuf).clear();
int size = reader.read(cbuf);
if (size <= 0) {
break;
}
cbuf.limit(cbuf.position());
cbuf.limit(((Buffer) cbuf).position());
cbuf.rewind();
boolean eof = false;
while (!eof) {
Expand All @@ -122,19 +123,19 @@ private void fillByteBuffer(Reader reader) throws IOException {
eof = true;
} else if (cr.isOverflow()) {
appendBytes(list, bbuf);
bbuf.clear();
((Buffer) bbuf).clear();
}
}
}
getByteArray(list);
}

private void appendBytes(List<byte[]> list, ByteBuffer bb) {
bb.flip();
int length = bb.limit();
((Buffer) bb).flip();
int length = ((Buffer) bb).limit();
totalBytes += length;
byte[] dst = new byte[length];
System.arraycopy(bb.array(), bb.position(), dst, 0, length);
System.arraycopy(bb.array(), ((Buffer) bb).position(), dst, 0, length);
list.add(dst);
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/embed/io/WriterOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -203,12 +204,12 @@ public void write(byte[] b, int off, int len) throws IOException {

private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException {
decoder.reset();
chars.clear();
((Buffer) chars).clear();
CoderResult result = decoder.decode(bytes, chars, true);
if (result.isError() || result.isOverflow()) {
throw new IOException(result.toString());
} else if (result.isUnderflow()) {
chars.flip();
((Buffer) chars).flip();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jruby.Ruby;
import jnr.posix.LibC;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;

Expand Down Expand Up @@ -74,7 +75,7 @@ public int read(ByteBuffer dst) throws IOException {
}
int n = libc.read(fd, dst, dst.remaining());
if (n > 0) {
dst.position(dst.position() + n);
dst.position(((Buffer) dst).position() + n);
} else if (n == 0) {
return -1; // EOF
}
Expand All @@ -95,7 +96,7 @@ public int write(ByteBuffer src) throws IOException {
}
int n = libc.write(fd, src, src.remaining());
if (n > 0) {
src.position(src.position() + n);
src.position(((Buffer) src).position() + n);
}
return n;
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.DatagramChannel;
Expand Down Expand Up @@ -491,7 +492,7 @@ protected ByteList doRead(ThreadContext context, final ByteBuffer buffer) {

if (read == 0) return null;

return new ByteList(buffer.array(), 0, buffer.position(), false);
return new ByteList(buffer.array(), 0, ((Buffer) buffer).position(), false);
}
catch (IOException e) {
// All errors to sysread should be SystemCallErrors, but on a closed stream
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/ext/socket/RubyUDPSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.net.StandardProtocolFamily;
import java.net.UnknownHostException;
import java.net.DatagramPacket;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AlreadyBoundException;
import java.nio.channels.Channel;
Expand Down Expand Up @@ -595,7 +596,7 @@ protected static IRubyObject doReceive(RubyBasicSocket socket, final Ruby runtim
}
}

RubyString result = runtime.newString(new ByteList(buf.array(), 0, buf.position(), false));
RubyString result = runtime.newString(new ByteList(buf.array(), 0, ((Buffer) buf).position(), false));

if (tuple != null) {
tuple.result = result;
Expand Down Expand Up @@ -626,8 +627,8 @@ private static IRubyObject doReceiveMulticast(RubyBasicSocket socket, final Ruby
throw runtime.newErrnoECONNRESETError();
}

recv.flip();
RubyString result = runtime.newString(new ByteList(recv.array(), recv.position(), recv.limit(), false));
((Buffer) recv).flip();
RubyString result = runtime.newString(new ByteList(recv.array(), ((Buffer) recv).position(), ((Buffer) recv).limit(), false));

if (tuple != null) {
tuple.result = result;
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.Channel;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

Expand Down Expand Up @@ -181,7 +182,7 @@ public IRubyObject send_io(ThreadContext context, IRubyObject arg) {
ByteBuffer[] outIov = new ByteBuffer[1];
outIov[0] = ByteBuffer.allocateDirect(dataBytes.length);
outIov[0].put(dataBytes);
outIov[0].flip();
((Buffer) outIov[0]).flip();

outMessage.setIov(outIov);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void addScopeInstructionOffset(IRScope scope) {
}

private int offset() {
return buf.position() + PROLOGUE_LENGTH;
return ((Buffer) buf).position() + PROLOGUE_LENGTH;
}

/**
Expand Down Expand Up @@ -276,8 +277,8 @@ public void endEncoding(IRScope script) {
stream.write(ByteBuffer.allocate(4).putInt(VERSION).array());
stream.write(ByteBuffer.allocate(4).putInt(headersOffset).array());
stream.write(ByteBuffer.allocate(4).putInt(poolOffset).array());
buf.flip();
stream.write(buf.array(), buf.position(), buf.limit());
((Buffer) buf).flip();
stream.write(buf.array(), ((Buffer) buf).position(), ((Buffer) buf).limit());
stream.close();
} catch (IOException e) {
try { if (stream != null) stream.close(); } catch (IOException e1) {}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/util/IOChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
package org.jruby.util;

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.ReadableByteChannel;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected int write(CallSite write, ByteBuffer src) throws IOException {
ByteList buffer;

if (src.hasArray()) {
buffer = new ByteList(src.array(), src.position(), src.remaining(), true);
buffer = new ByteList(src.array(), ((Buffer) src).position(), src.remaining(), true);
} else {
buffer = new ByteList(src.remaining());
buffer.append(src, src.remaining());
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package org.jruby.util;

import java.math.BigInteger;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

Expand Down Expand Up @@ -1096,7 +1097,7 @@ else if (encode.hasRemaining()) {
if (safeGet(encode) == '\n') {
safeGet(encode); // Possible Checksum Byte
} else if (encode.hasRemaining()) {
encode.position(encode.position() - 1);
encode.position(((Buffer) encode).position() - 1);
}
}
}
Expand Down Expand Up @@ -1192,7 +1193,7 @@ else if (encode.hasRemaining()) {
}
if ((s == '=') || c == -1) {
if (s == '=') {
encode.position(encode.position() - 1);
encode.position(((Buffer) encode).position() - 1);
}
break;
}
Expand All @@ -1205,7 +1206,7 @@ else if (encode.hasRemaining()) {
}
if ((s == '=') || d == -1) {
if (s == '=') {
encode.position(encode.position() - 1);
encode.position(((Buffer) encode).position() - 1);
}
break;
}
Expand Down Expand Up @@ -1298,7 +1299,7 @@ else if (encode.hasRemaining()) {
}

try {
encode.position(encode.position() - occurrences);
encode.position(((Buffer) encode).position() - occurrences);
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError("in `unpack': X outside of string");
}
Expand All @@ -1309,7 +1310,7 @@ else if (encode.hasRemaining()) {
}

try {
encode.position(encode.position() + occurrences);
encode.position(((Buffer) encode).position() + occurrences);
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError("in `unpack': x outside of string");
}
Expand All @@ -1323,7 +1324,7 @@ else if (encode.hasRemaining()) {
long ul = 0;
long ulmask = (0xfeL << 56) & 0xffffffff;
RubyBignum big128 = RubyBignum.newBignum(runtime, 128);
int pos = encode.position();
int pos = ((Buffer) encode).position();

while (occurrences > 0 && pos < encode.limit()) {
ul <<= 7;
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/util/ShellLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
Expand Down Expand Up @@ -1484,14 +1485,14 @@ private static class ChannelPumper extends Thread implements Pumper {
public void run() {
runtime.getCurrentContext().setEventHooksEnabled(false);
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
buf.clear();
((Buffer) buf).clear();
try {
while (!quit && inChannel.isOpen() && outChannel.isOpen()) {
int read = inChannel.read(buf);
if (read == -1) break;
buf.flip();
((Buffer) buf).flip();
outChannel.write(buf);
buf.clear();
((Buffer) buf).clear();
}
} catch (Exception e) {
} finally {
Expand Down
Loading

0 comments on commit 68f4722

Please sign in to comment.