Created
November 20, 2018 22:13
-
-
Save headius/08a568f5cf662ff207145e54cc0dbeb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/core/src/main/java/org/jruby/RubyEncoding.java b/core/src/main/java/org/jruby/RubyEncoding.java | |
index f327459c89..daa8b21acd 100755 | |
--- a/core/src/main/java/org/jruby/RubyEncoding.java | |
+++ b/core/src/main/java/org/jruby/RubyEncoding.java | |
@@ -58,6 +58,8 @@ import org.jruby.util.StringSupport; | |
import org.jruby.util.io.EncodingUtils; | |
import org.jruby.util.unsafe.UnsafeHolder; | |
+import static org.jruby.util.io.ChannelHelper.clearBuffer; | |
+ | |
@JRubyClass(name="Encoding") | |
public class RubyEncoding extends RubyObject implements Constantizable { | |
public static final Charset UTF8 = Charset.forName("UTF-8"); | |
@@ -301,10 +303,8 @@ public class RubyEncoding extends RubyObject implements Constantizable { | |
} | |
public final ByteBuffer encode(String str) { | |
- ByteBuffer buf = byteBuffer; | |
- CharBuffer cbuf = charBuffer; | |
- buf.clear(); | |
- cbuf.clear(); | |
+ ByteBuffer buf = clearBuffer(byteBuffer); | |
+ CharBuffer cbuf = clearBuffer(charBuffer); | |
cbuf.put(str); | |
cbuf.flip(); | |
encoder.encode(cbuf, buf, true); | |
@@ -314,10 +314,8 @@ public class RubyEncoding extends RubyObject implements Constantizable { | |
} | |
public final ByteBuffer encode(CharSequence str) { | |
- ByteBuffer buf = byteBuffer; | |
- CharBuffer cbuf = charBuffer; | |
- buf.clear(); | |
- cbuf.clear(); | |
+ ByteBuffer buf = clearBuffer(byteBuffer); | |
+ CharBuffer cbuf = clearBuffer(charBuffer); | |
// 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)); | |
@@ -329,10 +327,8 @@ public class RubyEncoding extends RubyObject implements Constantizable { | |
} | |
public final CharBuffer decode(byte[] bytes, int start, int length) { | |
- CharBuffer cbuf = charBuffer; | |
- ByteBuffer buf = byteBuffer; | |
- cbuf.clear(); | |
- buf.clear(); | |
+ ByteBuffer buf = clearBuffer(byteBuffer); | |
+ CharBuffer cbuf = clearBuffer(charBuffer); | |
buf.put(bytes, start, length); | |
buf.flip(); | |
decoder.decode(buf, cbuf, true); | |
diff --git a/core/src/main/java/org/jruby/RubyIO.java b/core/src/main/java/org/jruby/RubyIO.java | |
index d99135396f..07150f2223 100644 | |
--- a/core/src/main/java/org/jruby/RubyIO.java | |
+++ b/core/src/main/java/org/jruby/RubyIO.java | |
@@ -4445,7 +4445,7 @@ public class RubyIO extends RubyObject implements IOEncodable, Closeable, Flusha | |
buffer.flip(); | |
to.write(buffer); | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
transferred += n; | |
if (length > 0) { | |
diff --git a/core/src/main/java/org/jruby/embed/io/ReaderInputStream.java b/core/src/main/java/org/jruby/embed/io/ReaderInputStream.java | |
index 6713e7a726..542b9dbf77 100644 | |
--- a/core/src/main/java/org/jruby/embed/io/ReaderInputStream.java | |
+++ b/core/src/main/java/org/jruby/embed/io/ReaderInputStream.java | |
@@ -42,6 +42,8 @@ import java.nio.charset.CodingErrorAction; | |
import java.util.ArrayList; | |
import java.util.List; | |
+import static org.jruby.util.io.ChannelHelper.clearBuffer; | |
+ | |
/** | |
* A ReaderInputStream converts java.io.Reader to java.io.InputStream. The | |
* ReaderInputStream reads data in a given Reader object into its internal buffer | |
@@ -105,7 +107,7 @@ public class ReaderInputStream extends InputStream { | |
ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE); | |
List<byte[]> list = new ArrayList<byte[]>(); | |
while (true) { | |
- cbuf.clear(); | |
+ clearBuffer(cbuf); | |
int size = reader.read(cbuf); | |
if (size <= 0) { | |
break; | |
@@ -122,7 +124,7 @@ public class ReaderInputStream extends InputStream { | |
eof = true; | |
} else if (cr.isOverflow()) { | |
appendBytes(list, bbuf); | |
- bbuf.clear(); | |
+ clearBuffer(bbuf); | |
} | |
} | |
} | |
diff --git a/core/src/main/java/org/jruby/embed/io/WriterOutputStream.java b/core/src/main/java/org/jruby/embed/io/WriterOutputStream.java | |
index a51eff5963..0af8775bbb 100644 | |
--- a/core/src/main/java/org/jruby/embed/io/WriterOutputStream.java | |
+++ b/core/src/main/java/org/jruby/embed/io/WriterOutputStream.java | |
@@ -40,6 +40,8 @@ import java.nio.charset.CharsetDecoder; | |
import java.nio.charset.CoderResult; | |
import java.nio.charset.CodingErrorAction; | |
+import static org.jruby.util.io.ChannelHelper.clearBuffer; | |
+ | |
/** | |
* A WriterOutputStream converts java.io.Writer to java.io.OutputStream. | |
* | |
@@ -203,7 +205,7 @@ public class WriterOutputStream extends OutputStream { | |
private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException { | |
decoder.reset(); | |
- chars.clear(); | |
+ clearBuffer(chars); | |
CoderResult result = decoder.decode(bytes, chars, true); | |
if (result.isError() || result.isOverflow()) { | |
throw new IOException(result.toString()); | |
diff --git a/core/src/main/java/org/jruby/util/ShellLauncher.java b/core/src/main/java/org/jruby/util/ShellLauncher.java | |
index a715c9364b..3bed80baf8 100644 | |
--- a/core/src/main/java/org/jruby/util/ShellLauncher.java | |
+++ b/core/src/main/java/org/jruby/util/ShellLauncher.java | |
@@ -29,6 +29,7 @@ | |
package org.jruby.util; | |
import static java.lang.System.out; | |
+import static org.jruby.util.io.ChannelHelper.clearBuffer; | |
import java.io.File; | |
import java.io.FileInputStream; | |
@@ -1484,14 +1485,14 @@ public class ShellLauncher { | |
public void run() { | |
runtime.getCurrentContext().setEventHooksEnabled(false); | |
ByteBuffer buf = ByteBuffer.allocateDirect(1024); | |
- buf.clear(); | |
+ clearBuffer(buf); | |
try { | |
while (!quit && inChannel.isOpen() && outChannel.isOpen()) { | |
int read = inChannel.read(buf); | |
if (read == -1) break; | |
buf.flip(); | |
outChannel.write(buf); | |
- buf.clear(); | |
+ clearBuffer(buf); | |
} | |
} catch (Exception e) { | |
} finally { | |
diff --git a/core/src/main/java/org/jruby/util/io/ChannelHelper.java b/core/src/main/java/org/jruby/util/io/ChannelHelper.java | |
index 185f2fac1a..555b688dcd 100644 | |
--- a/core/src/main/java/org/jruby/util/io/ChannelHelper.java | |
+++ b/core/src/main/java/org/jruby/util/io/ChannelHelper.java | |
@@ -19,6 +19,7 @@ import java.io.FilterOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.lang.reflect.Field; | |
+import java.nio.Buffer; | |
import java.nio.channels.Channels; | |
import java.nio.channels.ReadableByteChannel; | |
import java.nio.channels.WritableByteChannel; | |
@@ -177,6 +178,16 @@ public abstract class ChannelHelper { | |
return filteredStream; | |
} | |
+ /** | |
+ * Call the Buffer.clear method by casting to Buffer, so it will continue to work on Java 8. | |
+ * | |
+ * @param buffer the buffer on which to call clear | |
+ * @return the original buffer reference | |
+ */ | |
+ public static <T extends Buffer> T clearBuffer(T buffer) { | |
+ return (T) buffer.clear(); | |
+ } | |
+ | |
private static OutputStream unwrapDripStream(OutputStream stream) { | |
if (isDripSwitchable(stream)) { | |
try { | |
diff --git a/core/src/main/java/org/jruby/util/io/ChannelStream.java b/core/src/main/java/org/jruby/util/io/ChannelStream.java | |
index 19198c76d0..ff0ffd5f1d 100644 | |
--- a/core/src/main/java/org/jruby/util/io/ChannelStream.java | |
+++ b/core/src/main/java/org/jruby/util/io/ChannelStream.java | |
@@ -57,6 +57,8 @@ import org.jruby.util.ResourceException; | |
import org.jruby.util.log.Logger; | |
import org.jruby.util.log.LoggerFactory; | |
+import static org.jruby.util.io.ChannelHelper.clearBuffer; | |
+ | |
/** | |
* This file implements a seekable IO file. | |
*/ | |
@@ -212,7 +214,7 @@ public class ChannelStream implements Stream, Finalizable { | |
@Override | |
public final int refillBuffer() throws IOException { | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
int n = ((ReadableByteChannel) descriptor.getChannel()).read(buffer); | |
buffer.flip(); | |
return n; | |
@@ -261,7 +263,7 @@ public class ChannelStream implements Stream, Finalizable { | |
// terminate and advance buffer when we find our char | |
buf.append(bytes, offset, i - offset); | |
if (i >= max) { | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
} else { | |
buffer.position(i + 1); | |
} | |
@@ -699,7 +701,7 @@ public class ChannelStream implements Stream, Finalizable { | |
if(n != len) { | |
// TODO: check the return value here | |
} | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
} | |
/** | |
@@ -735,7 +737,7 @@ public class ChannelStream implements Stream, Finalizable { | |
buffer.compact(); | |
return false; | |
} | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
return true; | |
} | |
@@ -804,7 +806,7 @@ public class ChannelStream implements Stream, Finalizable { | |
if (reading) { | |
// for SEEK_CUR, need to adjust for buffered data | |
adj = buffer.remaining(); | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
buffer.flip(); | |
} else { | |
flushWrite(); | |
@@ -846,7 +848,7 @@ public class ChannelStream implements Stream, Finalizable { | |
private void ensureRead() throws IOException, BadDescriptorException { | |
if (reading) return; | |
flushWrite(); | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
buffer.flip(); | |
reading = true; | |
} | |
@@ -868,7 +870,7 @@ public class ChannelStream implements Stream, Finalizable { | |
} else { | |
// libc flushes writes on any read from the actual file, so we flush here | |
flushWrite(); | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
buffer.flip(); | |
reading = true; | |
} | |
@@ -882,7 +884,7 @@ public class ChannelStream implements Stream, Finalizable { | |
} | |
} | |
// FIXME: Clearing read buffer here...is this appropriate? | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
reading = false; | |
} | |
@@ -1439,7 +1441,7 @@ public class ChannelStream implements Stream, Finalizable { | |
flushWrite(); | |
// reset buffer | |
- buffer.clear(); | |
+ clearBuffer(buffer); | |
if (reading) { | |
buffer.flip(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment