public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, boolean autoclose) throws InvalidValueException { // check these modes before constructing, so we don't finalize the partially-initialized stream descriptor.checkNewModes(modes); return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, autoclose), modes); }
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()) { // already have some bytes buffered, just return those return bufferedRead(Math.min(bufferedInputBytesRemaining(), number)); } else { // otherwise, we try an unbuffered read to get whatever's available return read(number); } }
public synchronized void fputc(int c) throws IOException, BadDescriptorException { bufferedWrite(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; }
/** * @throws IOException * @throws BadDescriptorException */ public synchronized int fflush() throws IOException, BadDescriptorException { checkWritable(); try { flushWrite(); } catch (EOFException eofe) { return -1; } return 0; }
/** * @throws IOException * @throws BadDescriptorException */ private int bufferedWrite(int c) throws IOException, BadDescriptorException { checkWritable(); ensureWrite(); if (!buffer.hasRemaining()) flushWrite(); buffer.put((byte) c); if (isSync()) flushWrite(); return 1; }
ensureRead(); final long left = fileSize - channel.position() + bufferedInputBytesRemaining(); if (left <= 0) { eof = true; if (getRuntime() != null) { throw getRuntime().newIOError("File too large"); } else { throw new IOException("File too large"); copyBufferedBytes(buf); return new ByteList(0); } else { checkReadable(); ByteList read = fread(BUFSIZE); read = fread(BUFSIZE);
private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException { checkReadable(); ensureRead(); bytesRead += copyBufferedBytes(dst); int read = refillBuffer(); } else { bytesRead += copyBufferedBytes(dst); throw newEOFException();
public synchronized int getline(ByteList dst, byte terminator) throws IOException, BadDescriptorException { checkReadable(); ensureRead(); descriptor.checkOpen(); boolean found = false; if (hasUngotChars()) { for(int i = 0; i < ungotChars.length(); i++){ byte ungotc = (byte) ungotChars.get(i); ++totalRead; clearUngotChars(); int n = refillBuffer(); if (n <= 0) { if (n < 0 && totalRead < 1) {
private ByteList bufferedRead(int number) throws IOException, BadDescriptorException { checkReadable(); ensureRead(); int resultSize = 0; // 128K seems to be the minimum at which the stat+seek is faster than reallocation final int BULK_THRESHOLD = 128 * 1024; if (number >= BULK_THRESHOLD && descriptor.isSeekable() && descriptor.getChannel() instanceof FileChannel) { // // If it is a file channel, then we can pre-allocate the output buffer // to the total size of buffered + remaining bytes in file // FileChannel fileChannel = (FileChannel) descriptor.getChannel(); resultSize = (int) Math.min(fileChannel.size() - fileChannel.position() + bufferedInputBytesRemaining(), number); } else { // // Cannot discern the total read length - allocate at least enough for the buffered data // resultSize = Math.min(bufferedInputBytesRemaining(), number); } ByteList result = new ByteList(resultSize); bufferedRead(result, number); return result; }
/** * Copies bytes from the channel buffer into a destination <tt>ByteBuffer</tt> * * @param dst A <tt>ByteList</tt> to place the data in. * @param len The maximum number of bytes to copy. * @return The number of bytes copied. */ private final int copyBufferedBytes(ByteList dst, int len) { int bytesCopied = 0; dst.ensure(Math.min(len, bufferedInputBytesRemaining())); if (hasUngotChars() && hasUngotChars()) { for(int i = 0; i < ungotChars.length(); i++){ byte ungotc = (byte) ungotChars.get(i); ++bytesCopied; dst.append(ungotc); } clearUngotChars(); } // // Copy out any buffered bytes // if (bytesCopied < len && buffer.hasRemaining()) { int n = Math.min(buffer.remaining(), len - bytesCopied); dst.append(buffer, n); bytesCopied += n; } return bytesCopied; }
bytesRead += copyBufferedBytes(dst, number); int read = refillBuffer(); throw newEOFException();
/** * Copies bytes from the channel buffer into a destination <tt>ByteBuffer</tt> * * @param dst A <tt>ByteBuffer</tt> to place the data in. * @return The number of bytes copied. */ 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; }
public synchronized ByteList read(int number) throws IOException, BadDescriptorException { checkReadable(); ensureReadNonBuffered(); ByteList byteList = new ByteList(number); // TODO this should entry into error handling somewhere int bytesRead = descriptor.read(number, byteList); if (bytesRead == -1) { eof = true; } return byteList; }
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; }
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; } }
/** * Closes IO handler resources. * * @throws IOException * @throws BadDescriptorException */ public void fclose() throws IOException, BadDescriptorException { try { synchronized (this) { closedExplicitly = true; close(); // not closing from finalize } } finally { Ruby localRuntime = getRuntime(); // Make sure we remove finalizers while not holding self lock, // otherwise there is a possibility for a deadlock! if (localRuntime != null) localRuntime.removeInternalFinalizer(this); // clear runtime so it doesn't get stuck in memory (JRUBY-2933) runtime = null; } }
public synchronized int read(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException, EOFException { assert dst.hasRemaining(); return bufferedRead(dst, partial); }
/** * @throws IOException * @throws BadDescriptorException */ public boolean feof() throws IOException, BadDescriptorException { checkReadable(); if (eof) { return true; } else { return false; } }
/** * @throws IOException * @throws BadDescriptorException */ private int bufferedWrite(int c) throws IOException, BadDescriptorException { checkWritable(); ensureWrite(); if (!buffer.hasRemaining()) flushWrite(); buffer.put((byte) c); if (isSync()) flushWrite(); return 1; }