/** * Check whether the target set of flags is a superset of this one; used to * ensure that a file is not re-opened with more privileges than it already * had. * * @param superset The ModeFlags object which should be a superset of this one * @return true if the object is a superset, false otherwise */ public boolean isSubsetOf(ModeFlags superset) { // TODO: Make sure all appropriate open flags are added to this check. if ((!superset.isReadable() && isReadable()) || (!superset.isWritable() && isWritable()) || !superset.isAppendable() && isAppendable()) { return false; } return true; }
@Override public String toString() { StringBuilder buf = new StringBuilder("ModeFlags(" + flags + "): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
/** * Check whether the target set of flags is a superset of this one; used to * ensure that a file is not re-opened with more privileges than it already * had. * * @param superset The ModeFlags object which should be a superset of this one * @return true if the object is a superset, false otherwise */ public boolean isSubsetOf(ModeFlags superset) { // TODO: Make sure all appropriate open flags are added to this check. if ((!superset.isReadable() && isReadable()) || (!superset.isWritable() && isWritable()) || !superset.isAppendable() && isAppendable()) { return false; } return true; }
/** * Check whether the target set of flags is a superset of this one; used to * ensure that a file is not re-opened with more privileges than it already * had. * * @param superset The ModeFlags object which should be a superset of this one * @return true if the object is a superset, false otherwise */ public boolean isSubsetOf(ModeFlags superset) { // TODO: Make sure all appropriate open flags are added to this check. if ((!superset.isReadable() && isReadable()) || (!superset.isWritable() && isWritable()) || !superset.isAppendable() && isAppendable()) { return false; } return true; }
/** * Check whether the target set of flags is a superset of this one; used to * ensure that a file is not re-opened with more privileges than it already * had. * * @param superset The ModeFlags object which should be a superset of this one * @return true if the object is a superset, false otherwise */ public boolean isSubsetOf(ModeFlags superset) { // TODO: Make sure all appropriate open flags are added to this check. if ((!superset.isReadable() && isReadable()) || (!superset.isWritable() && isWritable()) || !superset.isAppendable() && isAppendable()) { return false; } return true; }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("ModeFlags(").append(flags).append("): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTemporary()) buf.append("TMPFILE "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("ModeFlags(").append(flags).append("): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTemporary()) buf.append("TMPFILE "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
@Override public String toString() { StringBuilder buf = new StringBuilder("ModeFlags(" + flags + "): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
/** * Write the bytes in the specified byte list to the associated channel. * * @param buffer the byte list containing the bytes to be written * @return the number of bytes actually written * @throws java.io.IOException if there is an exception during IO * @throws org.jruby.util.io.BadDescriptorException if the associated * channel is already closed */ public int internalWrite(ByteBuffer buffer) throws IOException, BadDescriptorException { checkOpen(); // TODO: It would be nice to throw a better error for this if (!isWritable()) { throw new BadDescriptorException(); } WritableByteChannel writeChannel = (WritableByteChannel)channel; // if appendable, we always seek to the end before writing if (isSeekable() && originalModes.isAppendable()) { // if already in append mode, we don't do our own seeking if (!isInAppendMode) { FileChannel fileChannel = (FileChannel)channel; fileChannel.position(fileChannel.size()); } } return writeChannel.write(buffer); }
private Channel createChannel(ModeFlags flags) throws IOException { SeekableByteChannel fileChannel; try{ if (flags.isWritable() && !flags.isReadable()) { FileOutputStream fos = new FileOutputStream(file, flags.isAppendable()); fileChannel = fos.getChannel(); } else { RandomAccessFile raf = new RandomAccessFile(file, flags.toJavaModeString()); fileChannel = raf.getChannel(); // O_APPEND specifies that all writes will always be at the end of the open file // (even if we happened to have seek'd away from the end of the file o_O). RAF // does not have these semantics so we wrap it to support this unusual case. if (flags.isAppendable()) fileChannel = new AppendModeChannel((FileChannel) fileChannel); } } catch (FileNotFoundException ex) { throw mapFileNotFoundOnGetChannel(this, ex); } try { if (flags.isTruncate()) fileChannel.truncate(0); } catch (IOException ioe) { // ignore; it's a pipe or fifo that can't be truncated (we only care about illegal seek). if (!"Illegal seek".equals(ioe.getMessage())) throw ioe; } return fileChannel; }
private Channel createChannel(ModeFlags flags) throws IOException { FileChannel fileChannel; /* Because RandomAccessFile does not provide a way to pass append * mode, we must manually seek if using RAF. FileOutputStream, * however, does properly honor append mode at the lowest levels, * reducing append write costs when we're only doing writes. * * The code here will use a FileOutputStream if we're only writing, * setting isInAppendMode to true to disable our manual seeking. * * RandomAccessFile does not handle append for us, so if we must * also be readable we pass false for isInAppendMode to indicate * we need manual seeking. */ try{ if (flags.isWritable() && !flags.isReadable()) { FileOutputStream fos = new FileOutputStream(file, flags.isAppendable()); fileChannel = fos.getChannel(); } else { RandomAccessFile raf = new RandomAccessFile(file, flags.toJavaModeString()); fileChannel = raf.getChannel(); } } catch (FileNotFoundException ex) { throw mapFileNotFoundOnGetChannel(this, ex); } return fileChannel; }
private Channel createChannel(ModeFlags flags) throws IOException { SeekableByteChannel fileChannel; try{ if (flags.isWritable() && !flags.isReadable()) { FileOutputStream fos = new FileOutputStream(file, flags.isAppendable()); fileChannel = fos.getChannel(); } else { RandomAccessFile raf = new RandomAccessFile(file, flags.toJavaModeString()); fileChannel = raf.getChannel(); // O_APPEND specifies that all writes will always be at the end of the open file // (even if we happened to have seek'd away from the end of the file o_O). RAF // does not have these semantics so we wrap it to support this unusual case. if (flags.isAppendable()) fileChannel = new AppendModeChannel((FileChannel) fileChannel); } } catch (FileNotFoundException ex) { throw mapFileNotFoundOnGetChannel(this, ex); } try { if (flags.isTruncate()) fileChannel.truncate(0); } catch (IOException ioe) { // ignore; it's a pipe or fifo that can't be truncated (we only care about illegal seek). if (!"Illegal seek".equals(ioe.getMessage())) throw ioe; } return fileChannel; }
private Channel createChannel(ModeFlags flags) throws IOException { FileChannel fileChannel; /* Because RandomAccessFile does not provide a way to pass append * mode, we must manually seek if using RAF. FileOutputStream, * however, does properly honor append mode at the lowest levels, * reducing append write costs when we're only doing writes. * * The code here will use a FileOutputStream if we're only writing, * setting isInAppendMode to true to disable our manual seeking. * * RandomAccessFile does not handle append for us, so if we must * also be readable we pass false for isInAppendMode to indicate * we need manual seeking. */ try{ if (flags.isWritable() && !flags.isReadable()) { FileOutputStream fos = new FileOutputStream(file, flags.isAppendable()); fileChannel = fos.getChannel(); } else { RandomAccessFile raf = new RandomAccessFile(file, flags.toJavaModeString()); fileChannel = raf.getChannel(); } } catch (FileNotFoundException ex) { throw mapFileNotFoundOnGetChannel(this, ex); } return fileChannel; }
/** * Write the bytes in the specified byte list to the associated channel. * * @param buffer the byte list containing the bytes to be written * @return the number of bytes actually written * @throws java.io.IOException if there is an exception during IO * @throws org.jruby.util.io.BadDescriptorException if the associated * channel is already closed */ public int internalWrite(ByteBuffer buffer) throws IOException, BadDescriptorException { checkOpen(); // TODO: It would be nice to throw a better error for this if (!isWritable()) { throw new BadDescriptorException(); } WritableByteChannel writeChannel = (WritableByteChannel)channel; // if appendable, we always seek to the end before writing if (isSeekable() && originalModes.isAppendable()) { // if already in append mode, we don't do our own seeking if (!isInAppendMode) { FileChannel fileChannel = (FileChannel)channel; fileChannel.position(fileChannel.size()); } } return writeChannel.write(buffer); }
/** * Write the bytes in the specified byte list to the associated channel. * * @param buffer the byte list containing the bytes to be written * @return the number of bytes actually written * @throws java.io.IOException if there is an exception during IO * @throws org.jruby.util.io.BadDescriptorException if the associated * channel is already closed */ public int internalWrite(ByteBuffer buffer) throws IOException, BadDescriptorException { checkOpen(); // TODO: It would be nice to throw a better error for this if (!isWritable()) { throw new BadDescriptorException(); } WritableByteChannel writeChannel = (WritableByteChannel)channel; // if appendable, we always seek to the end before writing if (isSeekable() && originalModes.isAppendable()) { // if already in append mode, we don't do our own seeking if (!isInAppendMode) { SeekableByteChannel fileChannel = (SeekableByteChannel) channel; fileChannel.position(fileChannel.size()); } } return writeChannel.write(buffer); }
/** * Write the bytes in the specified byte list to the associated channel. * * @param buffer the byte list containing the bytes to be written * @return the number of bytes actually written * @throws java.io.IOException if there is an exception during IO * @throws org.jruby.util.io.BadDescriptorException if the associated * channel is already closed */ public int internalWrite(ByteBuffer buffer) throws IOException, BadDescriptorException { checkOpen(); // TODO: It would be nice to throw a better error for this if (!isWritable()) { throw new BadDescriptorException(); } WritableByteChannel writeChannel = (WritableByteChannel)channel; // if appendable, we always seek to the end before writing if (isSeekable() && originalModes.isAppendable()) { // if already in append mode, we don't do our own seeking if (!isInAppendMode) { SeekableByteChannel fileChannel = (SeekableByteChannel) channel; fileChannel.position(fileChannel.size()); } } return writeChannel.write(buffer); }
@JRubyMethod(name = "putc", required = 1) public IRubyObject putc(IRubyObject obj) { checkWritable(); byte c = RubyNumeric.num2chr(obj); checkFrozen(); ptr.string.modify(); ByteList bytes = ptr.string.getByteList(); if (ptr.modes.isAppendable()) { ptr.pos = bytes.length(); bytes.append(c); } else { if (isEndOfString()) bytes.length((int)ptr.pos + 1); bytes.set((int) ptr.pos, c); ptr.pos++; } return obj; }
@JRubyMethod(name = "putc", required = 1) public IRubyObject putc(IRubyObject obj) { checkWritable(); byte c = RubyNumeric.num2chr(obj); checkFrozen(); ptr.string.modify(); ByteList bytes = ptr.string.getByteList(); if (ptr.modes.isAppendable()) { ptr.pos = bytes.length(); bytes.append(c); } else { if (isEndOfString()) bytes.length((int)ptr.pos + 1); bytes.set((int) ptr.pos, c); ptr.pos++; } return obj; }
private int writeInternal18(ThreadContext context, IRubyObject arg) { checkWritable(); RubyString val = arg.asString(); ptr.string.modify(); if (ptr.modes.isAppendable()) { ptr.string.getByteList().append(val.getByteList()); ptr.pos = ptr.string.getByteList().length(); } else { int left = ptr.string.getByteList().length()-(int)ptr.pos; ptr.string.getByteList().replace((int)ptr.pos,Math.min(val.getByteList().length(),left),val.getByteList()); ptr.pos += val.getByteList().length(); } if (val.isTaint()) { ptr.string.setTaint(true); } return val.getByteList().length(); }
private int writeInternal18(ThreadContext context, IRubyObject arg) { checkWritable(); RubyString val = arg.asString(); ptr.string.modify(); if (ptr.modes.isAppendable()) { ptr.string.getByteList().append(val.getByteList()); ptr.pos = ptr.string.getByteList().length(); } else { int left = ptr.string.getByteList().length()-(int)ptr.pos; ptr.string.getByteList().replace((int)ptr.pos,Math.min(val.getByteList().length(),left),val.getByteList()); ptr.pos += val.getByteList().length(); } if (val.isTaint()) { ptr.string.setTaint(true); } return val.getByteList().length(); }