package org.jruby.util.io;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.SelectableChannel;
import org.jruby.Finalizable;
import org.jruby.Ruby;
import org.jruby.platform.Platform;
import org.jruby.util.ByteList;
import org.jruby.util.JRubyFile;
import org.jruby.util.ResourceException;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import static com.headius.backport9.buffer.Buffers.clearBuffer;
import static com.headius.backport9.buffer.Buffers.flipBuffer;
import static com.headius.backport9.buffer.Buffers.limitBuffer;
import static com.headius.backport9.buffer.Buffers.positionBuffer;
@Deprecated
public class ChannelStream implements Stream, Finalizable {
private static final Logger LOG = LoggerFactory.getLogger(ChannelStream.class);
private final static boolean DEBUG = false;
public final static int BUFSIZE = 4 * 1024;
private final static int BULK_READ_SIZE = 16 * 1024;
private final static ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private static EOFException eofException = new EOFException();
private volatile Ruby runtime;
protected ModeFlags modes;
protected boolean sync = false;
protected volatile ByteBuffer buffer;
protected boolean reading;
private ChannelDescriptor descriptor;
private boolean blocking = true;
private ByteList ungotChars = new ByteList();
private volatile boolean closedExplicitly = false;
private volatile boolean eof = false;
private volatile boolean autoclose = true;
private ChannelStream(Ruby runtime, ChannelDescriptor descriptor, boolean autoclose) {
this.runtime = runtime;
this.descriptor = descriptor;
this.modes = descriptor.getOriginalModes();
buffer = ByteBuffer.allocate(BUFSIZE);
flipBuffer(buffer);
this.reading = true;
this.autoclose = autoclose;
runtime.addInternalFinalizer(this);
}
private ChannelStream(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, boolean autoclose) {
this(runtime, descriptor, autoclose);
this.modes = modes;
}
@Override
public ByteBuffer getBuffer() {
return buffer;
}
public Ruby getRuntime() {
return runtime;
}
public void checkReadable() throws IOException {
if (!modes.isReadable()) throw new IOException("not opened for reading");
}
public void checkWritable() throws IOException {
if (!modes.isWritable()) throw new IOException("not opened for writing");
}
public void checkPermissionsSubsetOf(ModeFlags subsetModes) {
subsetModes.isSubsetOf(modes);
}
public ModeFlags getModes() {
return modes;
}
public void setModes(ModeFlags modes) {
this.modes = modes;
}
public boolean isSync() {
return sync;
}
public void setSync(boolean sync) {
this.sync = sync;
}
public void setBinmode() {
}
public boolean isBinmode() {
return false;
}
public boolean isAutoclose() {
return autoclose;
}
public void setAutoclose(boolean autoclose) {
this.autoclose = autoclose;
}
public void waitUntilReady() throws IOException, InterruptedException {
while (ready() == 0) {
Thread.sleep(10);
}
}
public final boolean readDataBuffered() {
return hasBufferedInputBytes();
}
private boolean hasUngotChars() {
return ungotChars.length() > 0;
}
public final boolean writeDataBuffered() {
return !reading && buffer.position() > 0;
}
@Override
public final int bufferedAvailable() {
return buffer.remaining();
}
@Override
public final int refillBuffer() throws IOException {
clearBuffer(buffer);
int n = ((ReadableByteChannel) descriptor.getChannel()).read(buffer);
flipBuffer(buffer);
return n;
}
public synchronized ByteList fgets(ByteList separatorString) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
if (separatorString == null) {
return readall();
}
final ByteList separator = (separatorString == PARAGRAPH_DELIMETER) ?
PARAGRAPH_SEPARATOR : separatorString;
descriptor.checkOpen();
if (feof()) {
return null;
}
int c = read();
if (c == -1) {
return null;
}
positionBuffer(buffer, buffer.position() - 1);
ByteList buf = new ByteList(40);
byte first = separator.getUnsafeBytes()[separator.getBegin()];
LineLoop : while (true) {
ReadLoop: while (true) {
byte[] bytes = buffer.array();
int offset = buffer.position();
int max = buffer.limit();
for (int i = offset; i < max; i++) {
c = bytes[i];
if (c == first) {
buf.append(bytes, offset, i - offset);
if (i >= max) {
clearBuffer(buffer);
} else {
positionBuffer(buffer, i + 1);
}
break ReadLoop;
}
}
buf.append(bytes, offset, buffer.remaining());
int read = refillBuffer();
if (read == -1) break LineLoop;
}
for (int i = 0; i < separator.getRealSize(); i++) {
if (c == -1) {
break LineLoop;
} else if (c != separator.getUnsafeBytes()[separator.getBegin() + i]) {
buf.append(c);
continue LineLoop;
}
buf.append(c);
if (i < separator.getRealSize() - 1) {
c = read();
}
}
break;
}
if (separatorString == PARAGRAPH_DELIMETER) {
while (c == separator.getUnsafeBytes()[separator.getBegin()]) {
c = read();
}
ungetc(c);
}
return buf;
}
public synchronized int getline(ByteList dst, byte terminator) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
descriptor.checkOpen();
int totalRead = 0;
boolean found = false;
if (hasUngotChars()) {
for(int i = 0; i < ungotChars.length(); i++){
byte ungotc = (byte) ungotChars.get(i);
dst.append(ungotc);
found = ungotc == terminator;
++totalRead;
}
clearUngotChars();
}
while (!found) {
final byte[] bytes = buffer.array();
final int begin = buffer.arrayOffset() + buffer.position();
final int end = begin + buffer.remaining();
int len = 0;
for (int i = begin; i < end && !found; ++i) {
found = bytes[i] == terminator;
++len;
}
if (len > 0) {
dst.append(buffer, len);
totalRead += len;
}
if (!found) {
int n = refillBuffer();
if (n <= 0) {
if (n < 0 && totalRead < 1) {
return -1;
}
break;
}
}
}
return totalRead;
}
public synchronized int getline(ByteList dst, byte terminator, long limit) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
descriptor.checkOpen();
int totalRead = 0;
boolean found = false;
if (hasUngotChars()) {
for(int i = 0; i < ungotChars.length(); i++){
byte ungotc = (byte) ungotChars.get(i);
dst.append(ungotc);
found = ungotc == terminator;
limit--;
++totalRead;
}
clearUngotChars();
}
while (!found) {
final byte[] bytes = buffer.array();
final int begin = buffer.arrayOffset() + buffer.position();
final int end = begin + buffer.remaining();
int len = 0;
for (int i = begin; i < end && limit-- > 0 && !found; ++i) {
found = bytes[i] == terminator;
++len;
}
if (limit < 1) found = true;
if (len > 0) {
dst.append(buffer, len);
totalRead += len;
}
if (!found) {
int n = refillBuffer();
if (n <= 0) {
if (n < 0 && totalRead < 1) {
return -1;
}
break;
}
}
}
return totalRead;
}
private void clearUngotChars() {
if(ungotChars.length() > 0) {
ungotChars.delete(0, ungotChars.length());
}
}
@Deprecated
public synchronized ByteList readall() throws IOException, BadDescriptorException {
final long fileSize = descriptor.isSeekable() && descriptor.getChannel() instanceof SeekableByteChannel
? ((SeekableByteChannel) descriptor.getChannel()).size() : 0;
if (fileSize > 0) {
ensureRead();
SeekableByteChannel channel = (SeekableByteChannel) descriptor.getChannel();
final long left = fileSize - channel.position() + bufferedInputBytesRemaining();
if (left <= 0) {
eof = true;
return null;
}
if (left > Integer.MAX_VALUE) {
if (getRuntime() != null) {
throw getRuntime().newIOError("File too large");
} else {
throw new IOException("File too large");
}
}
ByteList result = new ByteList((int) left);
ByteBuffer buf = ByteBuffer.wrap(result.getUnsafeBytes(),
result.begin(), (int) left);
copyBufferedBytes(buf);
while (buf.hasRemaining()) {
final int MAX_READ_CHUNK = 1 * 1024 * 1024;
ByteBuffer tmp = buf.duplicate();
if (tmp.remaining() > MAX_READ_CHUNK) {
limitBuffer(tmp, tmp.position() + MAX_READ_CHUNK);
}
int n = channel.read(tmp);
if (n <= 0) {
break;
}
positionBuffer(buf, tmp.position());
}
eof = true;
result.length(buf.position());
return result;
} else if (descriptor.isNull()) {
return new ByteList(0);
} else {
checkReadable();
ByteList byteList = new ByteList();
ByteList read = fread(BUFSIZE);
if (read == null) {
eof = true;
return byteList;
}
while (read != null) {
byteList.append(read);
read = fread(BUFSIZE);
}
return byteList;
}
}
private final int copyBufferedBytes(ByteBuffer dst) {
final int bytesToCopy = dst.remaining();
if (hasUngotChars() && dst.hasRemaining()) {
for(int i = 0; i < ungotChars.length(); i++){
byte ungotc = (byte) ungotChars.get(i);
dst.put(ungotc);
}
clearUngotChars();
}
if (buffer.hasRemaining() && dst.hasRemaining()) {
if (dst.remaining() >= buffer.remaining()) {
dst.put(buffer);
} else {
ByteBuffer tmp = buffer.duplicate();
limitBuffer(tmp, tmp.position() + dst.remaining());
dst.put(tmp);
positionBuffer(buffer, tmp.position());
}
}
return bytesToCopy - dst.remaining();
}
private final int copyBufferedBytes(byte[] dst, int off, int len) {
int bytesCopied = 0;
if (hasUngotChars() && len > 0) {
for(int i = 0; i < ungotChars.length(); i++){
byte ungotc = (byte) ungotChars.get(i);
dst[off++] = ungotc;
++bytesCopied;
}
clearUngotChars();
}
final int n = Math.min(len - bytesCopied, buffer.remaining());
buffer.get(dst, off, n);
bytesCopied += n;
return bytesCopied;
}
private final int copyBufferedBytes(ByteList dst, int len) {
int bytesCopied = 0;
dst.ensure(Math.min(len, bufferedInputBytesRemaining()));
if (hasUngotChars()) {
for(int i = 0; i < ungotChars.length(); i++){
byte ungotc = (byte) ungotChars.get(i);
++bytesCopied;
dst.append(ungotc);
}
clearUngotChars();
}
if (bytesCopied < len && buffer.hasRemaining()) {
int n = Math.min(buffer.remaining(), len - bytesCopied);
dst.append(buffer, n);
bytesCopied += n;
}
return bytesCopied;
}
private final int bufferedInputBytesRemaining() {
return reading ? (buffer.remaining() + (ungotChars.length())) : 0;
}
private final boolean hasBufferedInputBytes() {
return reading && (buffer.hasRemaining() || hasUngotChars());
}
private final int bufferedOutputSpaceRemaining() {
return !reading ? buffer.remaining() : 0;
}
private final boolean hasBufferedOutputSpace() {
return !reading && buffer.hasRemaining();
}
public void fclose() throws IOException, BadDescriptorException {
try {
synchronized (this) {
closedExplicitly = true;
close();
}
} finally {
Ruby localRuntime = getRuntime();
if (localRuntime != null) localRuntime.removeInternalFinalizer(this);
runtime = null;
}
}
private void close() throws IOException, BadDescriptorException {
finish(true);
}
private void finish(boolean close) throws BadDescriptorException, IOException {
try {
flushWrite();
if (DEBUG) LOG.info("Descriptor for fileno {} closed by stream", descriptor.getFileno());
} finally {
buffer = EMPTY_BUFFER;
runtime = null;
descriptor.finish(close);
}
}
public synchronized int fflush() throws IOException, BadDescriptorException {
checkWritable();
try {
flushWrite();
} catch (EOFException eofe) {
return -1;
}
return 0;
}
private void flushWrite() throws IOException, BadDescriptorException {
if (reading || !modes.isWritable() || buffer.position() == 0) return;
int len = buffer.position();
flipBuffer(buffer);
int n = descriptor.write(buffer);
if(n != len) {
}
clearBuffer(buffer);
}
private boolean flushWrite(final boolean block) throws IOException, BadDescriptorException {
if (reading || !modes.isWritable() || buffer.position() == 0) return false;
int len = buffer.position();
int nWritten = 0;
flipBuffer(buffer);
if (descriptor.getChannel() instanceof SelectableChannel) {
SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel();
synchronized (selectableChannel.blockingLock()) {
boolean oldBlocking = selectableChannel.isBlocking();
try {
if (oldBlocking != block) {
selectableChannel.configureBlocking(block);
}
nWritten = descriptor.write(buffer);
} finally {
if (oldBlocking != block) {
selectableChannel.configureBlocking(oldBlocking);
}
}
}
} else {
nWritten = descriptor.write(buffer);
}
if (nWritten != len) {
buffer.compact();
return false;
}
clearBuffer(buffer);
return true;
}
public InputStream newInputStream() {
InputStream in = descriptor.getBaseInputStream();
return in == null ? new InputStreamAdapter(this) : in;
}
public OutputStream newOutputStream() {
return new OutputStreamAdapter(this);
}
public void clearerr() {
eof = false;
}
public boolean feof() throws IOException, BadDescriptorException {
checkReadable();
if (eof) {
return true;
} else {
return false;
}
}
public synchronized long fgetpos() throws IOException, PipeException, InvalidValueException, BadDescriptorException {
if (descriptor.isSeekable()) {
SeekableByteChannel fileChannel = (SeekableByteChannel) descriptor.getChannel();
long pos = fileChannel.position();
if (reading) {
pos -= buffer.remaining();
return pos - (pos > 0 && hasUngotChars() ? ungotChars.length() : 0);
} else {
return pos + buffer.position();
}
} else if (descriptor.isNull()) {
return 0;
} else {
throw new PipeException();
}
}
public synchronized void lseek(long offset, int type) throws IOException, InvalidValueException, PipeException, BadDescriptorException {
if (descriptor.isSeekable()) {
SeekableByteChannel fileChannel = (SeekableByteChannel) descriptor.getChannel();
clearUngotChars();
int adj = 0;
if (reading) {
adj = buffer.remaining();
clearBuffer(buffer);
flipBuffer(buffer);
} else {
flushWrite();
}
try {
switch (type) {
case SEEK_SET:
fileChannel.position(offset);
break;
case SEEK_CUR:
fileChannel.position(fileChannel.position() - adj + offset);
break;
case SEEK_END:
fileChannel.position(fileChannel.size() + offset);
break;
}
} catch (IllegalArgumentException e) {
throw new InvalidValueException();
} catch (IOException ioe) {
throw ioe;
}
} else if (descriptor.getChannel() instanceof SelectableChannel) {
throw new PipeException();
} else {
}
}
public synchronized void sync() throws IOException, BadDescriptorException {
flushWrite();
}
private void ensureRead() throws IOException, BadDescriptorException {
if (reading) return;
flushWrite();
clearBuffer(buffer);
flipBuffer(buffer);
reading = true;
}
private void ensureReadNonBuffered() throws IOException, BadDescriptorException {
if (reading) {
if (buffer.hasRemaining()) {
Ruby localRuntime = getRuntime();
if (localRuntime != null) {
throw localRuntime.newIOError("sysread for buffered IO");
} else {
throw new IOException("sysread for buffered IO");
}
}
} else {
flushWrite();
clearBuffer(buffer);
flipBuffer(buffer);
reading = true;
}
}
private void resetForWrite() throws IOException {
if (descriptor.isSeekable()) {
SeekableByteChannel fileChannel = (SeekableByteChannel) descriptor.getChannel();
if (buffer.hasRemaining()) {
fileChannel.position(fileChannel.position() - buffer.remaining());
}
}
clearBuffer(buffer);
reading = false;
}
private void ensureWrite() throws IOException {
if (!reading) return;
resetForWrite();
}
public synchronized ByteList read(int number) throws IOException, BadDescriptorException {
checkReadable();
ensureReadNonBuffered();
ByteList byteList = new ByteList(number);
int bytesRead = descriptor.read(number, byteList);
if (bytesRead == -1) {
eof = true;
}
return byteList;
}
private ByteList bufferedRead(int number) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
int resultSize = 0;
final int BULK_THRESHOLD = 128 * 1024;
if (number >= BULK_THRESHOLD && descriptor.isSeekable() && descriptor.getChannel() instanceof SeekableByteChannel) {
SeekableByteChannel fileChannel = (SeekableByteChannel) descriptor.getChannel();
resultSize = (int) Math.min(fileChannel.size() - fileChannel.position() + bufferedInputBytesRemaining(), number);
} else {
resultSize = Math.min(bufferedInputBytesRemaining(), number);
}
ByteList result = new ByteList(resultSize);
bufferedRead(result, number);
return result;
}
private int bufferedRead(ByteList dst, int number) throws IOException, BadDescriptorException {
int bytesRead = 0;
bytesRead += copyBufferedBytes(dst, number);
boolean done = false;
while ((number - bytesRead) >= BUFSIZE) {
final int bytesToRead = Math.min(BULK_READ_SIZE, number - bytesRead);
final int n = descriptor.read(bytesToRead, dst);
if (n == -1) {
eof = true;
done = true;
break;
} else if (n == 0) {
done = true;
break;
}
bytesRead += n;
}
while (!done && bytesRead < number) {
int read = refillBuffer();
if (read == -1) {
eof = true;
break;
} else if (read == 0) {
break;
}
final int len = Math.min(buffer.remaining(), number - bytesRead);
dst.append(buffer, len);
bytesRead += len;
}
if (bytesRead == 0 && number != 0) {
if (eof) {
throw newEOFException();
}
}
return bytesRead;
}
private EOFException newEOFException() {
if (eofException != null) {
return eofException;
} else {
return new EOFException();
}
}
private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
boolean done = false;
int bytesRead = 0;
bytesRead += copyBufferedBytes(dst);
while ((bytesRead < 1 || !partial) && (dst.remaining() >= BUFSIZE || dst.isDirect())) {
ByteBuffer tmpDst = dst;
if (!dst.isDirect()) {
int bytesToRead = Math.min(BULK_READ_SIZE, dst.remaining());
if (bytesToRead < dst.remaining()) {
tmpDst = dst.duplicate();
limitBuffer(tmpDst, tmpDst.position() + bytesToRead);
}
}
int n = descriptor.read(tmpDst);
if (n == -1) {
eof = true;
done = true;
break;
} else if (n == 0) {
done = true;
break;
} else {
bytesRead += n;
}
}
while (!done && dst.hasRemaining() && (bytesRead < 1 || !partial)) {
int read = refillBuffer();
if (read == -1) {
eof = true;
done = true;
break;
} else if (read == 0) {
done = true;
break;
} else {
bytesRead += copyBufferedBytes(dst);
}
}
if (eof && bytesRead == 0 && dst.remaining() != 0) {
throw newEOFException();
}
return bytesRead;
}
private int bufferedRead() throws IOException, BadDescriptorException {
ensureRead();
if (!buffer.hasRemaining()) {
int len = refillBuffer();
if (len == -1) {
eof = true;
return -1;
} else if (len == 0) {
return -1;
}
}
return buffer.get() & 0xFF;
}
private int bufferedWrite(ByteList buf) throws IOException, BadDescriptorException {
checkWritable();
ensureWrite();
if (buf == null || buf.length() == 0) return 0;
if (buf.length() > buffer.capacity()) {
flushWrite();
int n = descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
if(n != buf.length()) {
}
} else {
if (buf.length() > buffer.remaining()) flushWrite();
buffer.put(buf.getUnsafeBytes(), buf.begin(), buf.length());
}
if (isSync()) flushWrite();
return buf.getRealSize();
}
private int bufferedWrite(ByteBuffer buf) throws IOException, BadDescriptorException {
checkWritable();
ensureWrite();
if (buf == null || !buf.hasRemaining()) return 0;
final int nbytes = buf.remaining();
if (nbytes >= buffer.capacity()) {
flushWrite();
descriptor.write(buf);
} else {
if (nbytes > buffer.remaining()) flushWrite();
buffer.put(buf);
}
if (isSync()) flushWrite();
return nbytes - buf.remaining();
}
private int bufferedWrite(int c) throws IOException, BadDescriptorException {
checkWritable();
ensureWrite();
if (!buffer.hasRemaining()) flushWrite();
buffer.put((byte) c);
if (isSync()) flushWrite();
return 1;
}
public synchronized void ftruncate(long newLength) throws IOException,
BadDescriptorException, InvalidValueException {
Channel ch = descriptor.getChannel();
if (!(ch instanceof SeekableByteChannel)) {
throw new InvalidValueException();
}
invalidateBuffer();
SeekableByteChannel fileChannel = (SeekableByteChannel) ch;
long position = fileChannel.position();
if (newLength > fileChannel.size()) {
int difference = (int)(newLength - fileChannel.size());
fileChannel.position(fileChannel.size());
fileChannel.write(ByteBuffer.allocate(difference));
} else {
fileChannel.truncate(newLength);
}
fileChannel.position(position);
}
private void invalidateBuffer() throws IOException, BadDescriptorException {
if (!reading) flushWrite();
int posOverrun = buffer.remaining();
clearBuffer(buffer);
if (reading) {
flipBuffer(buffer);
SeekableByteChannel fileChannel = (SeekableByteChannel) descriptor.getChannel();
if (posOverrun != 0) fileChannel.position(fileChannel.position() - posOverrun);
}
}
@Override
public void finalize() throws Throwable {
super.finalize();
if (closedExplicitly) return;
if (DEBUG) {
LOG.info("finalize() for not explicitly closed stream");
}
if (descriptor != null && descriptor.isOpen()) {
finish(autoclose);
}
}
public int ready() throws IOException {
if (descriptor.getChannel() instanceof SelectableChannel) {
int ready_stat = 0;
java.nio.channels.Selector sel = SelectorFactory.openWithRetryFrom(null, ((SelectableChannel) descriptor.getChannel()).provider());
SelectableChannel selchan = (SelectableChannel)descriptor.getChannel();
synchronized (selchan.blockingLock()) {
boolean is_block = selchan.isBlocking();
try {
selchan.configureBlocking(false);
selchan.register(sel, java.nio.channels.SelectionKey.OP_READ);
ready_stat = sel.selectNow();
sel.close();
} catch (Throwable ex) {
} finally {
if (sel != null) {
try {
sel.close();
} catch (Exception e) {
}
}
selchan.configureBlocking(is_block);
}
}
return ready_stat;
} else {
return newInputStream().available();
}
}
public synchronized void fputc(int c) throws IOException, BadDescriptorException {
bufferedWrite(c);
}
public int ungetc(int c) {
if (c == -1) {
return -1;
}
eof = false;
ungotChars.prepend((byte)c);
return c;
}
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();
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 {
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 SeekableByteChannel) {
return fread(number);
} else {
return null;
}
}
public synchronized ByteList readpartial(int number) throws IOException, BadDescriptorException, EOFException {
assert number >= 0;
if (number == 0) {
return null;
}
if (descriptor.getChannel() instanceof SeekableByteChannel) {
return fread(number);
}
if (hasBufferedInputBytes()) {
return bufferedRead(Math.min(bufferedInputBytesRemaining(), number));
} else {
return read(number);
}
}
public synchronized int read(ByteBuffer dst) throws IOException, BadDescriptorException, EOFException {
return read(dst, !(descriptor.getChannel() instanceof SeekableByteChannel));
}
public synchronized int read(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException, EOFException {
assert dst.hasRemaining();
return bufferedRead(dst, partial);
}
public synchronized int read() throws IOException, BadDescriptorException {
try {
descriptor.checkOpen();
if (hasUngotChars()) {
int c = ungotChars.get(0);
ungotChars.delete(0,1);
return c;
}
return bufferedRead();
} catch (EOFException e) {
eof = true;
return -1;
}
}
public ChannelDescriptor getDescriptor() {
return descriptor;
}
public void setBlocking(boolean block) throws IOException {
if (!(descriptor.getChannel() instanceof SelectableChannel)) {
return;
}
synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) {
blocking = block;
try {
((SelectableChannel) descriptor.getChannel()).configureBlocking(block);
} catch (IllegalBlockingModeException e) {
}
}
}
public boolean isBlocking() {
return blocking;
}
public synchronized void freopen(Ruby runtime, String path, ModeFlags modes) throws DirectoryAsFileException, IOException, InvalidValueException, PipeException, BadDescriptorException {
flushWrite();
clearBuffer(buffer);
if (reading) {
flipBuffer(buffer);
}
this.modes = modes;
if (descriptor.isOpen()) {
descriptor.close();
}
if (path.equals("/dev/null") || path.equalsIgnoreCase("nul:") || path.equalsIgnoreCase("nul")) {
descriptor = descriptor.reopen(new NullChannel(), modes);
} else {
String cwd = runtime.getCurrentDirectory();
JRubyFile theFile = JRubyFile.create(cwd,path);
if (theFile.isDirectory() && modes.isWritable()) {
throw runtime.newErrnoEISDirError(path);
}
if (modes.isCreate()) {
if (theFile.exists() && modes.isExclusive()) {
throw runtime.newErrnoEEXISTError("File exists - " + path);
}
theFile.createNewFile();
} else {
if (!theFile.exists()) {
throw runtime.newErrnoENOENTError("file not found - " + path);
}
}
RandomAccessFile file = new RandomAccessFile(theFile, modes.toJavaModeString());
if (modes.isTruncate()) file.setLength(0L);
descriptor = descriptor.reopen(file, modes);
try {
if (modes.isAppendable()) lseek(0, SEEK_END);
} catch (PipeException pe) {
}
}
}
public static Stream open(Ruby runtime, ChannelDescriptor descriptor) {
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, true), descriptor.getOriginalModes());
}
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes) throws InvalidValueException {
descriptor.checkNewModes(modes);
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, true), modes);
}
public static Stream open(Ruby runtime, ChannelDescriptor descriptor, boolean autoclose) {
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, autoclose), descriptor.getOriginalModes());
}
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, boolean autoclose) throws InvalidValueException {
descriptor.checkNewModes(modes);
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, autoclose), modes);
}
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) {
if (modes.isText() ||
(Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof SeekableByteChannel && !modes.isBinary())) {
return new CRLFStreamWrapper(stream);
}
return stream;
}
@Deprecated
public static Stream fopen(Ruby runtime, String path, ModeFlags modes) throws FileNotFoundException, DirectoryAsFileException, FileExistsException, IOException, InvalidValueException, PipeException, BadDescriptorException {
try {
ChannelDescriptor descriptor = ChannelDescriptor.open(runtime.getCurrentDirectory(), path, modes, runtime.getClassLoader());
Stream stream = fdopen(runtime, descriptor, modes);
return stream;
} catch (ResourceException resourceException) {
throw resourceException.newRaiseException(runtime);
}
}
public Channel getChannel() {
return getDescriptor().getChannel();
}
private static final class InputStreamAdapter extends java.io.InputStream {
private final ChannelStream stream;
public InputStreamAdapter(ChannelStream stream) {
this.stream = stream;
}
@Override
public int read() throws IOException {
synchronized (stream) {
if (stream.hasBufferedInputBytes()) {
try {
return stream.read();
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
}
}
}
byte[] b = new byte[1];
return read(b, 0, 1) == 1 ? b[0] & 0xff: -1;
}
@Override
public int read(byte[] bytes, int off, int len) throws IOException {
if (bytes == null) {
throw new NullPointerException("null destination buffer");
}
if ((len | off | (off + len) | (bytes.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}
try {
synchronized(stream) {
final int available = stream.bufferedInputBytesRemaining();
if (available >= len) {
return stream.copyBufferedBytes(bytes, off, len);
} else if (stream.getDescriptor().getChannel() instanceof SelectableChannel) {
SelectableChannel ch = (SelectableChannel) stream.getDescriptor().getChannel();
synchronized (ch.blockingLock()) {
boolean oldBlocking = ch.isBlocking();
try {
if (!oldBlocking) {
ch.configureBlocking(true);
}
return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
} finally {
if (!oldBlocking) {
ch.configureBlocking(oldBlocking);
}
}
}
} else {
return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
}
}
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
} catch (EOFException ex) {
return -1;
}
}
@Override
public int available() throws IOException {
synchronized (stream) {
return !stream.eof ? stream.bufferedInputBytesRemaining() : 0;
}
}
@Override
public void close() throws IOException {
try {
synchronized (stream) {
stream.fclose();
}
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
}
}
}
private static final class OutputStreamAdapter extends java.io.OutputStream {
private final ChannelStream stream;
public OutputStreamAdapter(ChannelStream stream) {
this.stream = stream;
}
@Override
public void write(int i) throws IOException {
synchronized (stream) {
if (!stream.isSync() && stream.hasBufferedOutputSpace()) {
stream.buffer.put((byte) i);
return;
}
}
byte[] b = { (byte) i };
write(b, 0, 1);
}
@Override
public void write(byte[] bytes, int off, int len) throws IOException {
if (bytes == null) {
throw new NullPointerException("null source buffer");
}
if ((len | off | (off + len) | (bytes.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
}
try {
synchronized(stream) {
if (!stream.isSync() && stream.bufferedOutputSpaceRemaining() >= len) {
stream.buffer.put(bytes, off, len);
} else if (stream.getDescriptor().getChannel() instanceof SelectableChannel) {
SelectableChannel ch = (SelectableChannel) stream.getDescriptor().getChannel();
synchronized (ch.blockingLock()) {
boolean oldBlocking = ch.isBlocking();
try {
if (!oldBlocking) {
ch.configureBlocking(true);
}
stream.bufferedWrite(ByteBuffer.wrap(bytes, off, len));
} finally {
if (!oldBlocking) {
ch.configureBlocking(oldBlocking);
}
}
}
} else {
stream.bufferedWrite(ByteBuffer.wrap(bytes, off, len));
}
}
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
}
}
@Override
public void close() throws IOException {
try {
synchronized (stream) {
stream.fclose();
}
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
}
}
@Override
public void flush() throws IOException {
try {
synchronized (stream) {
stream.flushWrite(true);
}
} catch (BadDescriptorException ex) {
throw new IOException(ex.getMessage());
}
}
}
}