Refine search
public OutputStream compress(OutputStream out) throws IOException { return new GZIPOutputStream(new BufferedOutputStream(out)); } };
@Override @JRubyMethod(name = "close") public IRubyObject close() { if (!closed) { try { io.close(); if (realIo.respondsTo("close")) { realIo.callMethod(realIo.getRuntime().getCurrentContext(), "close"); } } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } this.closed = true; return realIo; }
@JRubyMethod(name = "flush", optional = 1) public IRubyObject flush(IRubyObject[] args) { int flush = JZlib.Z_SYNC_FLUSH; if (args.length > 0 && !args[0].isNil()) { flush = RubyNumeric.fix2int(args[0]); } boolean tmp = io.getSyncFlush(); try { if (flush != 0 /* * NO_FLUSH */) { io.setSyncFlush(true); } io.flush(); } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } finally { io.setSyncFlush(tmp); } return getRuntime().getNil(); }
@JRubyMethod(name = "mtime=", required = 1) public IRubyObject set_mtime(IRubyObject arg) { if (arg instanceof RubyTime) { this.mtime = ((RubyTime) arg); } else if (arg.isNil()) { // ...nothing } else { this.mtime.setDateTime(new DateTime(RubyNumeric.fix2long(arg) * 1000)); } try { io.setModifiedTime(this.mtime.to_i().getLongValue()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return getRuntime().getNil(); }
@JRubyMethod(name = "orig_name=", required = 1) public IRubyObject set_orig_name(IRubyObject obj) { nullFreeOrigName = obj.convertToString(); ensureNonNull(nullFreeOrigName); try { io.setName(nullFreeOrigName.toString()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return obj; }
@JRubyMethod(name = "comment=", required = 1) public IRubyObject set_comment(IRubyObject obj) { nullFreeComment = obj.convertToString(); ensureNonNull(nullFreeComment); try { io.setComment(nullFreeComment.toString()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return obj; }
private IRubyObject initializeCommon(IRubyObject stream, int level) { realIo = (RubyObject) stream; try { // the 15+16 here is copied from a Deflater default constructor Deflater deflater = new Deflater(level, 15+16, false); final IOOutputStream ioOutputStream = new IOOutputStream(realIo, false, false) { /** * Customize IOOutputStream#write(byte[], int, int) to create a defensive copy of the byte array * that GZIPOutputStream hands us. * * That byte array is a reference to one of GZIPOutputStream's internal byte buffers. * The base IOOutputStream#write(byte[], int, int) uses the bytes it is handed to back a * copy-on-write ByteList. So, without this defensive copy, those two classes overwrite each * other's bytes, corrupting our output. */ @Override public void write(byte[] bytes, int off, int len) throws IOException { byte[] bytesCopy = new byte[len]; System.arraycopy(bytes, off, bytesCopy, 0, len); super.write(bytesCopy, 0, len); } }; io = new GZIPOutputStream(ioOutputStream, deflater, 512, false); return this; } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } }
@Override public IRubyObject finish() { if (!finished) { try { io.finish(); } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } finished = true; return realIo; }
@JRubyMethod(name = {"pos", "tell"}) public IRubyObject pos() { return RubyNumeric.int2fix(getRuntime(), io.getTotalIn()); }
@Override @JRubyMethod(name = "close") public IRubyObject close() { if (!closed) { try { io.close(); if (realIo.respondsTo("close")) { realIo.callMethod(realIo.getRuntime().getCurrentContext(), "close"); } } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } this.closed = true; return realIo; }
@JRubyMethod(name = "flush", optional = 1) public IRubyObject flush(IRubyObject[] args) { int flush = JZlib.Z_SYNC_FLUSH; if (args.length > 0 && !args[0].isNil()) { flush = RubyNumeric.fix2int(args[0]); } boolean tmp = io.getSyncFlush(); try { if (flush != 0 /* * NO_FLUSH */) { io.setSyncFlush(true); } io.flush(); } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } finally { io.setSyncFlush(tmp); } return getRuntime().getNil(); }
@JRubyMethod(name = "mtime=", required = 1) public IRubyObject set_mtime(IRubyObject arg) { if (arg instanceof RubyTime) { this.mtime = ((RubyTime) arg); } else if (arg.isNil()) { // ...nothing } else { this.mtime.setDateTime(new DateTime(RubyNumeric.fix2long(arg) * 1000)); } try { io.setModifiedTime(this.mtime.to_i().getLongValue()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return getRuntime().getNil(); }
@JRubyMethod(name = "orig_name=", required = 1) public IRubyObject set_orig_name(IRubyObject obj) { nullFreeOrigName = obj.convertToString(); ensureNonNull(nullFreeOrigName); try { io.setName(nullFreeOrigName.toString()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return obj; }
@JRubyMethod(name = "comment=", required = 1) public IRubyObject set_comment(IRubyObject obj) { nullFreeComment = obj.convertToString(); ensureNonNull(nullFreeComment); try { io.setComment(nullFreeComment.toString()); } catch (GZIPException e) { throw RubyZlib.newGzipFileError(getRuntime(), "header is already written"); } return obj; }
private IRubyObject initializeCommon(IRubyObject stream, int level) { realIo = (RubyObject) stream; try { // the 15+16 here is copied from a Deflater default constructor Deflater deflater = new Deflater(level, 15+16, false); final IOOutputStream ioOutputStream = new IOOutputStream(realIo, false, false) { /** * Customize IOOutputStream#write(byte[], int, int) to create a defensive copy of the byte array * that GZIPOutputStream hands us. * * That byte array is a reference to one of GZIPOutputStream's internal byte buffers. * The base IOOutputStream#write(byte[], int, int) uses the bytes it is handed to back a * copy-on-write ByteList. So, without this defensive copy, those two classes overwrite each * other's bytes, corrupting our output. */ @Override public void write(byte[] bytes, int off, int len) throws IOException { byte[] bytesCopy = new byte[len]; System.arraycopy(bytes, off, bytesCopy, 0, len); super.write(bytesCopy, 0, len); } }; io = new GZIPOutputStream(ioOutputStream, deflater, 512, false); return this; } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } }
@Override public IRubyObject finish() { if (!finished) { try { io.finish(); } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } finished = true; return realIo; }
@JRubyMethod(name = {"pos", "tell"}) public IRubyObject pos() { return RubyNumeric.int2fix(getRuntime(), io.getTotalIn()); }
@Override @JRubyMethod(name = "close") public IRubyObject close() { if (!closed) { try { io.close(); if (realIo.respondsTo("close")) { realIo.callMethod(realIo.getRuntime().getCurrentContext(), "close"); } } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } this.closed = true; return getRuntime().getNil(); }