Source Code Cross Referenced for RubyZlib.java in  » Scripting » jruby » org » jruby » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » jruby » org.jruby 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /***** BEGIN LICENSE BLOCK *****
0002:         * Version: CPL 1.0/GPL 2.0/LGPL 2.1
0003:         *
0004:         * The contents of this file are subject to the Common Public
0005:         * License Version 1.0 (the "License"); you may not use this file
0006:         * except in compliance with the License. You may obtain a copy of
0007:         * the License at http://www.eclipse.org/legal/cpl-v10.html
0008:         *
0009:         * Software distributed under the License is distributed on an "AS
0010:         * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
0011:         * implied. See the License for the specific language governing
0012:         * rights and limitations under the License.
0013:         *
0014:         * Copyright (C) 2006 Ola Bini <ola@ologix.com>
0015:         * 
0016:         * Alternatively, the contents of this file may be used under the terms of
0017:         * either of the GNU General Public License Version 2 or later (the "GPL"),
0018:         * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
0019:         * in which case the provisions of the GPL or the LGPL are applicable instead
0020:         * of those above. If you wish to allow use of your version of this file only
0021:         * under the terms of either the GPL or the LGPL, and not to allow others to
0022:         * use your version of this file under the terms of the CPL, indicate your
0023:         * decision by deleting the provisions above and replace them with the notice
0024:         * and other provisions required by the GPL or the LGPL. If you do not delete
0025:         * the provisions above, a recipient may use your version of this file under
0026:         * the terms of any one of the CPL, the GPL or the LGPL.
0027:         ***** END LICENSE BLOCK *****/package org.jruby;
0028:
0029:        import java.io.InputStream;
0030:        import java.io.IOException;
0031:
0032:        import java.util.List;
0033:        import java.util.ArrayList;
0034:
0035:        import java.util.zip.GZIPInputStream;
0036:        import java.util.zip.GZIPOutputStream;
0037:
0038:        import org.jruby.exceptions.RaiseException;
0039:        import org.jruby.runtime.Arity;
0040:
0041:        import org.jruby.runtime.Block;
0042:        import org.jruby.runtime.CallbackFactory;
0043:        import org.jruby.runtime.ObjectAllocator;
0044:        import org.jruby.runtime.ThreadContext;
0045:        import org.jruby.runtime.builtin.IRubyObject;
0046:
0047:        import org.jruby.util.IOInputStream;
0048:        import org.jruby.util.IOOutputStream;
0049:        import org.jruby.util.CRC32Ext;
0050:        import org.jruby.util.Adler32Ext;
0051:        import org.jruby.util.ZlibInflate;
0052:        import org.jruby.util.ZlibDeflate;
0053:
0054:        import org.jruby.util.ByteList;
0055:
0056:        public class RubyZlib {
0057:            /** Create the Zlib module and add it to the Ruby runtime.
0058:             * 
0059:             */
0060:            public static RubyModule createZlibModule(Ruby runtime) {
0061:                RubyModule result = runtime.defineModule("Zlib");
0062:
0063:                RubyClass gzfile = result.defineClassUnder("GzipFile", runtime
0064:                        .getObject(), RubyGzipFile.GZIPFILE_ALLOCATOR);
0065:                CallbackFactory callbackFactory = runtime
0066:                        .callbackFactory(RubyGzipFile.class);
0067:                gzfile.getMetaClass().defineMethod(
0068:                        "wrap",
0069:                        callbackFactory.getSingletonMethod("wrap",
0070:                                RubyGzipFile.class, RubyKernel.IRUBY_OBJECT));
0071:                gzfile.getMetaClass().defineMethod("new",
0072:                        callbackFactory.getSingletonMethod("newInstance"));
0073:                gzfile.defineFastMethod("os_code", callbackFactory
0074:                        .getFastMethod("os_code"));
0075:                gzfile.defineFastMethod("closed?", callbackFactory
0076:                        .getFastMethod("closed_p"));
0077:                gzfile.defineFastMethod("orig_name", callbackFactory
0078:                        .getFastMethod("orig_name"));
0079:                gzfile.defineFastMethod("to_io", callbackFactory
0080:                        .getFastMethod("to_io"));
0081:                gzfile.defineFastMethod("finish", callbackFactory
0082:                        .getFastMethod("finish"));
0083:                gzfile.defineFastMethod("comment", callbackFactory
0084:                        .getFastMethod("comment"));
0085:                gzfile.defineFastMethod("crc", callbackFactory
0086:                        .getFastMethod("crc"));
0087:                gzfile.defineFastMethod("mtime", callbackFactory
0088:                        .getFastMethod("mtime"));
0089:                gzfile.defineFastMethod("sync", callbackFactory
0090:                        .getFastMethod("sync"));
0091:                gzfile.defineFastMethod("close", callbackFactory
0092:                        .getFastMethod("close"));
0093:                gzfile.defineFastMethod("level", callbackFactory
0094:                        .getFastMethod("level"));
0095:                gzfile.defineFastMethod("sync=", callbackFactory.getFastMethod(
0096:                        "set_sync", RubyKernel.IRUBY_OBJECT));
0097:
0098:                CallbackFactory classCB = runtime
0099:                        .callbackFactory(RubyClass.class);
0100:                RubyClass gzreader = result.defineClassUnder("GzipReader",
0101:                        gzfile, RubyGzipReader.GZIPREADER_ALLOCATOR);
0102:                gzreader.includeModule(runtime.getModule("Enumerable"));
0103:                CallbackFactory callbackFactory2 = runtime
0104:                        .callbackFactory(RubyGzipReader.class);
0105:                gzreader.getMetaClass().defineMethod(
0106:                        "open",
0107:                        callbackFactory2.getSingletonMethod("open",
0108:                                RubyString.class));
0109:                gzreader.getMetaClass().defineMethod("new",
0110:                        classCB.getOptMethod("newInstance"));
0111:                gzreader.defineMethod("initialize", callbackFactory2.getMethod(
0112:                        "initialize", RubyKernel.IRUBY_OBJECT));
0113:                gzreader.defineFastMethod("rewind", callbackFactory2
0114:                        .getFastMethod("rewind"));
0115:                gzreader.defineFastMethod("lineno", callbackFactory2
0116:                        .getFastMethod("lineno"));
0117:                gzreader.defineFastMethod("readline", callbackFactory2
0118:                        .getFastMethod("readline"));
0119:                gzreader.defineFastMethod("read", callbackFactory2
0120:                        .getFastOptMethod("read"));
0121:                gzreader.defineFastMethod("lineno=", callbackFactory2
0122:                        .getFastMethod("set_lineno", RubyNumeric.class));
0123:                gzreader.defineFastMethod("pos", callbackFactory2
0124:                        .getFastMethod("pos"));
0125:                gzreader.defineFastMethod("readchar", callbackFactory2
0126:                        .getFastMethod("readchar"));
0127:                gzreader.defineFastMethod("readlines", callbackFactory2
0128:                        .getFastOptMethod("readlines"));
0129:                gzreader.defineMethod("each_byte", callbackFactory2
0130:                        .getMethod("each_byte"));
0131:                gzreader.defineFastMethod("getc", callbackFactory2
0132:                        .getFastMethod("getc"));
0133:                gzreader.defineFastMethod("eof", callbackFactory2
0134:                        .getFastMethod("eof"));
0135:                gzreader.defineFastMethod("ungetc", callbackFactory2
0136:                        .getFastMethod("ungetc", RubyNumeric.class));
0137:                gzreader.defineMethod("each", callbackFactory2
0138:                        .getOptMethod("each"));
0139:                gzreader.defineFastMethod("unused", callbackFactory2
0140:                        .getFastMethod("unused"));
0141:                gzreader.defineFastMethod("eof?", callbackFactory2
0142:                        .getFastMethod("eof_p"));
0143:                gzreader.defineFastMethod("gets", callbackFactory2
0144:                        .getFastOptMethod("gets"));
0145:                gzreader.defineFastMethod("tell", callbackFactory2
0146:                        .getFastMethod("tell"));
0147:
0148:                RubyClass standardError = runtime.getClass("StandardError");
0149:                RubyClass zlibError = result.defineClassUnder("Error",
0150:                        standardError, standardError.getAllocator());
0151:                gzreader.defineClassUnder("Error", zlibError, zlibError
0152:                        .getAllocator());
0153:
0154:                RubyClass gzwriter = result.defineClassUnder("GzipWriter",
0155:                        gzfile, RubyGzipWriter.GZIPWRITER_ALLOCATOR);
0156:                CallbackFactory callbackFactory3 = runtime
0157:                        .callbackFactory(RubyGzipWriter.class);
0158:                gzwriter.getMetaClass().defineMethod("open",
0159:                        callbackFactory3.getOptSingletonMethod("open"));
0160:                gzwriter.getMetaClass().defineMethod("new",
0161:                        classCB.getOptMethod("newInstance"));
0162:                gzwriter.defineMethod("initialize", callbackFactory3
0163:                        .getOptMethod("initialize2"));
0164:                gzwriter.defineFastMethod("<<", callbackFactory3.getFastMethod(
0165:                        "append", RubyKernel.IRUBY_OBJECT));
0166:                gzwriter.defineFastMethod("printf", callbackFactory3
0167:                        .getFastOptMethod("printf"));
0168:                gzwriter.defineFastMethod("pos", callbackFactory3
0169:                        .getFastMethod("pos"));
0170:                gzwriter.defineFastMethod("orig_name=", callbackFactory3
0171:                        .getFastMethod("set_orig_name", RubyString.class));
0172:                gzwriter.defineFastMethod("putc", callbackFactory3
0173:                        .getFastMethod("putc", RubyNumeric.class));
0174:                gzwriter.defineFastMethod("comment=", callbackFactory3
0175:                        .getFastMethod("set_comment", RubyString.class));
0176:                gzwriter.defineFastMethod("puts", callbackFactory3
0177:                        .getFastOptMethod("puts"));
0178:                gzwriter.defineFastMethod("flush", callbackFactory3
0179:                        .getFastOptMethod("flush"));
0180:                gzwriter.defineFastMethod("mtime=", callbackFactory3
0181:                        .getFastMethod("set_mtime", RubyKernel.IRUBY_OBJECT));
0182:                gzwriter.defineFastMethod("tell", callbackFactory3
0183:                        .getFastMethod("tell"));
0184:                gzwriter.defineFastMethod("write", callbackFactory3
0185:                        .getFastMethod("write", RubyKernel.IRUBY_OBJECT));
0186:
0187:                result.defineConstant("ZLIB_VERSION", runtime
0188:                        .newString("1.2.1"));
0189:                result.defineConstant("VERSION", runtime.newString("0.6.0"));
0190:
0191:                result.defineConstant("BINARY", runtime.newFixnum(0));
0192:                result.defineConstant("ASCII", runtime.newFixnum(1));
0193:                result.defineConstant("UNKNOWN", runtime.newFixnum(2));
0194:
0195:                result.defineConstant("DEF_MEM_LEVEL", runtime.newFixnum(8));
0196:                result.defineConstant("MAX_MEM_LEVEL", runtime.newFixnum(9));
0197:
0198:                result.defineConstant("OS_UNIX", runtime.newFixnum(3));
0199:                result.defineConstant("OS_UNKNOWN", runtime.newFixnum(255));
0200:                result.defineConstant("OS_CODE", runtime.newFixnum(11));
0201:                result.defineConstant("OS_ZSYSTEM", runtime.newFixnum(8));
0202:                result.defineConstant("OS_VMCMS", runtime.newFixnum(4));
0203:                result.defineConstant("OS_VMS", runtime.newFixnum(2));
0204:                result.defineConstant("OS_RISCOS", runtime.newFixnum(13));
0205:                result.defineConstant("OS_MACOS", runtime.newFixnum(7));
0206:                result.defineConstant("OS_OS2", runtime.newFixnum(6));
0207:                result.defineConstant("OS_AMIGA", runtime.newFixnum(1));
0208:                result.defineConstant("OS_QDOS", runtime.newFixnum(12));
0209:                result.defineConstant("OS_WIN32", runtime.newFixnum(11));
0210:                result.defineConstant("OS_ATARI", runtime.newFixnum(5));
0211:                result.defineConstant("OS_MSDOS", runtime.newFixnum(0));
0212:                result.defineConstant("OS_CPM", runtime.newFixnum(9));
0213:                result.defineConstant("OS_TOPS20", runtime.newFixnum(10));
0214:
0215:                result.defineConstant("DEFAULT_STRATEGY", runtime.newFixnum(0));
0216:                result.defineConstant("FILTERED", runtime.newFixnum(1));
0217:                result.defineConstant("HUFFMAN_ONLY", runtime.newFixnum(2));
0218:
0219:                result.defineConstant("NO_FLUSH", runtime.newFixnum(0));
0220:                result.defineConstant("SYNC_FLUSH", runtime.newFixnum(2));
0221:                result.defineConstant("FULL_FLUSH", runtime.newFixnum(3));
0222:                result.defineConstant("FINISH", runtime.newFixnum(4));
0223:
0224:                result.defineConstant("NO_COMPRESSION", runtime.newFixnum(0));
0225:                result.defineConstant("BEST_SPEED", runtime.newFixnum(1));
0226:                result.defineConstant("DEFAULT_COMPRESSION", runtime
0227:                        .newFixnum(-1));
0228:                result.defineConstant("BEST_COMPRESSION", runtime.newFixnum(9));
0229:
0230:                result.defineConstant("MAX_WBITS", runtime.newFixnum(15));
0231:
0232:                CallbackFactory cf = runtime.callbackFactory(RubyZlib.class);
0233:                result.defineFastModuleFunction("zlib_version", cf
0234:                        .getFastSingletonMethod("zlib_version"));
0235:                result.defineFastModuleFunction("version", cf
0236:                        .getFastSingletonMethod("version"));
0237:                result.defineFastModuleFunction("adler32", cf
0238:                        .getFastOptSingletonMethod("adler32"));
0239:                result.defineFastModuleFunction("crc32", cf
0240:                        .getFastOptSingletonMethod("crc32"));
0241:                result.defineFastModuleFunction("crc_table", cf
0242:                        .getFastSingletonMethod("crc_table"));
0243:
0244:                result.defineClassUnder("StreamEnd", zlibError, zlibError
0245:                        .getAllocator());
0246:                result.defineClassUnder("StreamError", zlibError, zlibError
0247:                        .getAllocator());
0248:                result.defineClassUnder("BufError", zlibError, zlibError
0249:                        .getAllocator());
0250:                result.defineClassUnder("NeedDict", zlibError, zlibError
0251:                        .getAllocator());
0252:                result.defineClassUnder("MemError", zlibError, zlibError
0253:                        .getAllocator());
0254:                result.defineClassUnder("VersionError", zlibError, zlibError
0255:                        .getAllocator());
0256:                result.defineClassUnder("DataError", zlibError, zlibError
0257:                        .getAllocator());
0258:
0259:                RubyClass gzError = gzfile.defineClassUnder("Error", zlibError,
0260:                        zlibError.getAllocator());
0261:                gzfile.defineClassUnder("CRCError", gzError, gzError
0262:                        .getAllocator());
0263:                gzfile.defineClassUnder("NoFooter", gzError, gzError
0264:                        .getAllocator());
0265:                gzfile.defineClassUnder("LengthError", gzError, gzError
0266:                        .getAllocator());
0267:
0268:                // ZStream actually *isn't* allocatable
0269:                RubyClass zstream = result
0270:                        .defineClassUnder("ZStream", runtime.getObject(),
0271:                                ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
0272:                CallbackFactory zstreamcb = runtime
0273:                        .callbackFactory(ZStream.class);
0274:                zstream.defineMethod("initialize", zstreamcb
0275:                        .getMethod("initialize"));
0276:                zstream.defineFastMethod("flush_next_out", zstreamcb
0277:                        .getFastMethod("flush_next_out"));
0278:                zstream.defineFastMethod("total_out", zstreamcb
0279:                        .getFastMethod("total_out"));
0280:                zstream.defineFastMethod("stream_end?", zstreamcb
0281:                        .getFastMethod("stream_end_p"));
0282:                zstream.defineFastMethod("data_type", zstreamcb
0283:                        .getFastMethod("data_type"));
0284:                zstream.defineFastMethod("closed?", zstreamcb
0285:                        .getFastMethod("closed_p"));
0286:                zstream.defineFastMethod("ended?", zstreamcb
0287:                        .getFastMethod("ended_p"));
0288:                zstream.defineFastMethod("end", zstreamcb.getFastMethod("end"));
0289:                zstream.defineFastMethod("reset", zstreamcb
0290:                        .getFastMethod("reset"));
0291:                zstream.defineFastMethod("avail_out", zstreamcb
0292:                        .getFastMethod("avail_out"));
0293:                zstream.defineFastMethod("avail_out=", zstreamcb.getFastMethod(
0294:                        "set_avail_out", RubyKernel.IRUBY_OBJECT));
0295:                zstream.defineFastMethod("adler", zstreamcb
0296:                        .getFastMethod("adler"));
0297:                zstream.defineFastMethod("finish", zstreamcb
0298:                        .getFastMethod("finish"));
0299:                zstream.defineFastMethod("avail_in", zstreamcb
0300:                        .getFastMethod("avail_in"));
0301:                zstream.defineFastMethod("flush_next_in", zstreamcb
0302:                        .getFastMethod("flush_next_in"));
0303:                zstream.defineFastMethod("total_in", zstreamcb
0304:                        .getFastMethod("total_in"));
0305:                zstream.defineFastMethod("finished?", zstreamcb
0306:                        .getFastMethod("finished_p"));
0307:                zstream.defineFastMethod("close", zstreamcb
0308:                        .getFastMethod("close"));
0309:                zstream.undefineMethod("new");
0310:
0311:                RubyClass infl = result.defineClassUnder("Inflate", zstream,
0312:                        Inflate.INFLATE_ALLOCATOR);
0313:                CallbackFactory inflcb = runtime.callbackFactory(Inflate.class);
0314:                infl.getMetaClass().defineFastMethod(
0315:                        "inflate",
0316:                        inflcb.getFastSingletonMethod("s_inflate",
0317:                                RubyKernel.IRUBY_OBJECT));
0318:                infl.defineFastMethod("initialize", inflcb
0319:                        .getFastOptMethod("_initialize"));
0320:                infl.defineFastMethod("<<", inflcb.getFastMethod("append",
0321:                        RubyKernel.IRUBY_OBJECT));
0322:                infl.defineFastMethod("sync_point?", inflcb
0323:                        .getFastMethod("sync_point_p"));
0324:                infl.defineFastMethod("set_dictionary", inflcb.getFastMethod(
0325:                        "set_dictionary", RubyKernel.IRUBY_OBJECT));
0326:                infl.defineFastMethod("inflate", inflcb.getFastMethod(
0327:                        "inflate", RubyKernel.IRUBY_OBJECT));
0328:                infl.defineFastMethod("sync", inflcb.getFastMethod("sync",
0329:                        RubyKernel.IRUBY_OBJECT));
0330:
0331:                RubyClass defl = result.defineClassUnder("Deflate", zstream,
0332:                        Deflate.DEFLATE_ALLOCATOR);
0333:                CallbackFactory deflcb = runtime.callbackFactory(Deflate.class);
0334:                defl.getMetaClass().defineFastMethod("deflate",
0335:                        deflcb.getFastOptSingletonMethod("s_deflate"));
0336:                defl.defineFastMethod("initialize", deflcb
0337:                        .getFastOptMethod("_initialize"));
0338:                defl.defineFastMethod("<<", deflcb.getFastMethod("append",
0339:                        RubyKernel.IRUBY_OBJECT));
0340:                defl.defineFastMethod("params", deflcb.getFastMethod("params",
0341:                        RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT));
0342:                defl.defineFastMethod("set_dictionary", deflcb.getFastMethod(
0343:                        "set_dictionary", RubyKernel.IRUBY_OBJECT));
0344:                defl
0345:                        .defineFastMethod("flush", deflcb
0346:                                .getFastOptMethod("flush"));
0347:                defl.defineFastMethod("deflate", deflcb
0348:                        .getFastOptMethod("deflate"));
0349:
0350:                runtime.getModule("Kernel").callMethod(
0351:                        runtime.getCurrentContext(), "require",
0352:                        runtime.newString("stringio"));
0353:
0354:                return result;
0355:            }
0356:
0357:            public static IRubyObject zlib_version(IRubyObject recv) {
0358:                return ((RubyModule) recv).getConstant("ZLIB_VERSION");
0359:            }
0360:
0361:            public static IRubyObject version(IRubyObject recv) {
0362:                return ((RubyModule) recv).getConstant("VERSION");
0363:            }
0364:
0365:            public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args)
0366:                    throws Exception {
0367:                args = Arity.scanArgs(recv.getRuntime(), args, 0, 2);
0368:                int crc = 0;
0369:                ByteList bytes = null;
0370:
0371:                if (!args[0].isNil())
0372:                    bytes = args[0].convertToString().getByteList();
0373:                if (!args[1].isNil())
0374:                    crc = RubyNumeric.fix2int(args[1]);
0375:
0376:                CRC32Ext ext = new CRC32Ext(crc);
0377:                if (bytes != null) {
0378:                    ext.update(bytes.unsafeBytes(), bytes.begin(), bytes
0379:                            .length());
0380:                }
0381:
0382:                return recv.getRuntime().newFixnum(ext.getValue());
0383:            }
0384:
0385:            public static IRubyObject adler32(IRubyObject recv,
0386:                    IRubyObject[] args) throws Exception {
0387:                args = Arity.scanArgs(recv.getRuntime(), args, 0, 2);
0388:                int adler = 1;
0389:                ByteList bytes = null;
0390:                if (!args[0].isNil())
0391:                    bytes = args[0].convertToString().getByteList();
0392:                if (!args[1].isNil())
0393:                    adler = RubyNumeric.fix2int(args[1]);
0394:
0395:                Adler32Ext ext = new Adler32Ext(adler);
0396:                if (bytes != null) {
0397:                    ext.update(bytes.unsafeBytes(), bytes.begin(), bytes
0398:                            .length()); // it's safe since adler.update doesn't modify the array
0399:                }
0400:                return recv.getRuntime().newFixnum(ext.getValue());
0401:            }
0402:
0403:            private final static long[] crctab = new long[] { 0L, 1996959894L,
0404:                    3993919788L, 2567524794L, 124634137L, 1886057615L,
0405:                    3915621685L, 2657392035L, 249268274L, 2044508324L,
0406:                    3772115230L, 2547177864L, 162941995L, 2125561021L,
0407:                    3887607047L, 2428444049L, 498536548L, 1789927666L,
0408:                    4089016648L, 2227061214L, 450548861L, 1843258603L,
0409:                    4107580753L, 2211677639L, 325883990L, 1684777152L,
0410:                    4251122042L, 2321926636L, 335633487L, 1661365465L,
0411:                    4195302755L, 2366115317L, 997073096L, 1281953886L,
0412:                    3579855332L, 2724688242L, 1006888145L, 1258607687L,
0413:                    3524101629L, 2768942443L, 901097722L, 1119000684L,
0414:                    3686517206L, 2898065728L, 853044451L, 1172266101L,
0415:                    3705015759L, 2882616665L, 651767980L, 1373503546L,
0416:                    3369554304L, 3218104598L, 565507253L, 1454621731L,
0417:                    3485111705L, 3099436303L, 671266974L, 1594198024L,
0418:                    3322730930L, 2970347812L, 795835527L, 1483230225L,
0419:                    3244367275L, 3060149565L, 1994146192L, 31158534L,
0420:                    2563907772L, 4023717930L, 1907459465L, 112637215L,
0421:                    2680153253L, 3904427059L, 2013776290L, 251722036L,
0422:                    2517215374L, 3775830040L, 2137656763L, 141376813L,
0423:                    2439277719L, 3865271297L, 1802195444L, 476864866L,
0424:                    2238001368L, 4066508878L, 1812370925L, 453092731L,
0425:                    2181625025L, 4111451223L, 1706088902L, 314042704L,
0426:                    2344532202L, 4240017532L, 1658658271L, 366619977L,
0427:                    2362670323L, 4224994405L, 1303535960L, 984961486L,
0428:                    2747007092L, 3569037538L, 1256170817L, 1037604311L,
0429:                    2765210733L, 3554079995L, 1131014506L, 879679996L,
0430:                    2909243462L, 3663771856L, 1141124467L, 855842277L,
0431:                    2852801631L, 3708648649L, 1342533948L, 654459306L,
0432:                    3188396048L, 3373015174L, 1466479909L, 544179635L,
0433:                    3110523913L, 3462522015L, 1591671054L, 702138776L,
0434:                    2966460450L, 3352799412L, 1504918807L, 783551873L,
0435:                    3082640443L, 3233442989L, 3988292384L, 2596254646L,
0436:                    62317068L, 1957810842L, 3939845945L, 2647816111L,
0437:                    81470997L, 1943803523L, 3814918930L, 2489596804L,
0438:                    225274430L, 2053790376L, 3826175755L, 2466906013L,
0439:                    167816743L, 2097651377L, 4027552580L, 2265490386L,
0440:                    503444072L, 1762050814L, 4150417245L, 2154129355L,
0441:                    426522225L, 1852507879L, 4275313526L, 2312317920L,
0442:                    282753626L, 1742555852L, 4189708143L, 2394877945L,
0443:                    397917763L, 1622183637L, 3604390888L, 2714866558L,
0444:                    953729732L, 1340076626L, 3518719985L, 2797360999L,
0445:                    1068828381L, 1219638859L, 3624741850L, 2936675148L,
0446:                    906185462L, 1090812512L, 3747672003L, 2825379669L,
0447:                    829329135L, 1181335161L, 3412177804L, 3160834842L,
0448:                    628085408L, 1382605366L, 3423369109L, 3138078467L,
0449:                    570562233L, 1426400815L, 3317316542L, 2998733608L,
0450:                    733239954L, 1555261956L, 3268935591L, 3050360625L,
0451:                    752459403L, 1541320221L, 2607071920L, 3965973030L,
0452:                    1969922972L, 40735498L, 2617837225L, 3943577151L,
0453:                    1913087877L, 83908371L, 2512341634L, 3803740692L,
0454:                    2075208622L, 213261112L, 2463272603L, 3855990285L,
0455:                    2094854071L, 198958881L, 2262029012L, 4057260610L,
0456:                    1759359992L, 534414190L, 2176718541L, 4139329115L,
0457:                    1873836001L, 414664567L, 2282248934L, 4279200368L,
0458:                    1711684554L, 285281116L, 2405801727L, 4167216745L,
0459:                    1634467795L, 376229701L, 2685067896L, 3608007406L,
0460:                    1308918612L, 956543938L, 2808555105L, 3495958263L,
0461:                    1231636301L, 1047427035L, 2932959818L, 3654703836L,
0462:                    1088359270L, 936918000L, 2847714899L, 3736837829L,
0463:                    1202900863L, 817233897L, 3183342108L, 3401237130L,
0464:                    1404277552L, 615818150L, 3134207493L, 3453421203L,
0465:                    1423857449L, 601450431L, 3009837614L, 3294710456L,
0466:                    1567103746L, 711928724L, 3020668471L, 3272380065L,
0467:                    1510334235L, 755167117 };
0468:
0469:            public static IRubyObject crc_table(IRubyObject recv) {
0470:                List ll = new ArrayList(crctab.length);
0471:                for (int i = 0; i < crctab.length; i++) {
0472:                    ll.add(recv.getRuntime().newFixnum(crctab[i]));
0473:                }
0474:                return recv.getRuntime().newArray(ll);
0475:            }
0476:
0477:            public static abstract class ZStream extends RubyObject {
0478:                protected boolean closed = false;
0479:                protected boolean ended = false;
0480:                protected boolean finished = false;
0481:
0482:                protected abstract int internalTotalOut();
0483:
0484:                protected abstract boolean internalStreamEndP();
0485:
0486:                protected abstract void internalEnd();
0487:
0488:                protected abstract void internalReset();
0489:
0490:                protected abstract int internalAdler();
0491:
0492:                protected abstract IRubyObject internalFinish()
0493:                        throws Exception;
0494:
0495:                protected abstract int internalTotalIn();
0496:
0497:                protected abstract void internalClose();
0498:
0499:                public ZStream(Ruby runtime, RubyClass type) {
0500:                    super (runtime, type);
0501:                }
0502:
0503:                public IRubyObject initialize(Block unusedBlock) {
0504:                    return this ;
0505:                }
0506:
0507:                public IRubyObject flush_next_out() {
0508:                    return getRuntime().getNil();
0509:                }
0510:
0511:                public IRubyObject total_out() {
0512:                    return getRuntime().newFixnum(internalTotalOut());
0513:                }
0514:
0515:                public IRubyObject stream_end_p() {
0516:                    return internalStreamEndP() ? getRuntime().getTrue()
0517:                            : getRuntime().getFalse();
0518:                }
0519:
0520:                public IRubyObject data_type() {
0521:                    return getRuntime().getModule("Zlib")
0522:                            .getConstant("UNKNOWN");
0523:                }
0524:
0525:                public IRubyObject closed_p() {
0526:                    return closed ? getRuntime().getTrue() : getRuntime()
0527:                            .getFalse();
0528:                }
0529:
0530:                public IRubyObject ended_p() {
0531:                    return ended ? getRuntime().getTrue() : getRuntime()
0532:                            .getFalse();
0533:                }
0534:
0535:                public IRubyObject end() {
0536:                    if (!ended) {
0537:                        internalEnd();
0538:                        ended = true;
0539:                    }
0540:                    return getRuntime().getNil();
0541:                }
0542:
0543:                public IRubyObject reset() {
0544:                    internalReset();
0545:                    return getRuntime().getNil();
0546:                }
0547:
0548:                public IRubyObject avail_out() {
0549:                    return RubyFixnum.zero(getRuntime());
0550:                }
0551:
0552:                public IRubyObject set_avail_out(IRubyObject p1) {
0553:                    return p1;
0554:                }
0555:
0556:                public IRubyObject adler() {
0557:                    return getRuntime().newFixnum(internalAdler());
0558:                }
0559:
0560:                public IRubyObject finish() throws Exception {
0561:                    if (!finished) {
0562:                        finished = true;
0563:                        return internalFinish();
0564:                    }
0565:                    return getRuntime().newString("");
0566:                }
0567:
0568:                public IRubyObject avail_in() {
0569:                    return RubyFixnum.zero(getRuntime());
0570:                }
0571:
0572:                public IRubyObject flush_next_in() {
0573:                    return getRuntime().getNil();
0574:                }
0575:
0576:                public IRubyObject total_in() {
0577:                    return getRuntime().newFixnum(internalTotalIn());
0578:                }
0579:
0580:                public IRubyObject finished_p() {
0581:                    return finished ? getRuntime().getTrue() : getRuntime()
0582:                            .getFalse();
0583:                }
0584:
0585:                public IRubyObject close() {
0586:                    if (!closed) {
0587:                        internalClose();
0588:                        closed = true;
0589:                    }
0590:                    return getRuntime().getNil();
0591:                }
0592:            }
0593:
0594:            public static class Inflate extends ZStream {
0595:                protected static ObjectAllocator INFLATE_ALLOCATOR = new ObjectAllocator() {
0596:                    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
0597:                        return new Inflate(runtime, klass);
0598:                    }
0599:                };
0600:
0601:                public static IRubyObject s_inflate(IRubyObject recv,
0602:                        IRubyObject string) throws Exception {
0603:                    return ZlibInflate.s_inflate(recv, string.convertToString()
0604:                            .getByteList());
0605:                }
0606:
0607:                public Inflate(Ruby runtime, RubyClass type) {
0608:                    super (runtime, type);
0609:                }
0610:
0611:                private ZlibInflate infl;
0612:
0613:                public IRubyObject _initialize(IRubyObject[] args)
0614:                        throws Exception {
0615:                    infl = new ZlibInflate(this );
0616:                    return this ;
0617:                }
0618:
0619:                public IRubyObject append(IRubyObject arg) {
0620:                    infl.append(arg);
0621:                    return this ;
0622:                }
0623:
0624:                public IRubyObject sync_point_p() {
0625:                    return infl.sync_point();
0626:                }
0627:
0628:                public IRubyObject set_dictionary(IRubyObject arg)
0629:                        throws Exception {
0630:                    return infl.set_dictionary(arg);
0631:                }
0632:
0633:                public IRubyObject inflate(IRubyObject string) throws Exception {
0634:                    return infl.inflate(string.convertToString().getByteList());
0635:                }
0636:
0637:                public IRubyObject sync(IRubyObject string) {
0638:                    return infl.sync(string);
0639:                }
0640:
0641:                protected int internalTotalOut() {
0642:                    return infl.getInflater().getTotalOut();
0643:                }
0644:
0645:                protected boolean internalStreamEndP() {
0646:                    return infl.getInflater().finished();
0647:                }
0648:
0649:                protected void internalEnd() {
0650:                    infl.getInflater().end();
0651:                }
0652:
0653:                protected void internalReset() {
0654:                    infl.getInflater().reset();
0655:                }
0656:
0657:                protected int internalAdler() {
0658:                    return infl.getInflater().getAdler();
0659:                }
0660:
0661:                protected IRubyObject internalFinish() throws Exception {
0662:                    infl.finish();
0663:                    return getRuntime().getNil();
0664:                }
0665:
0666:                public IRubyObject finished_p() {
0667:                    return infl.getInflater().finished() ? getRuntime()
0668:                            .getTrue() : getRuntime().getFalse();
0669:                }
0670:
0671:                protected int internalTotalIn() {
0672:                    return infl.getInflater().getTotalIn();
0673:                }
0674:
0675:                protected void internalClose() {
0676:                    infl.close();
0677:                }
0678:            }
0679:
0680:            public static class Deflate extends ZStream {
0681:                protected static ObjectAllocator DEFLATE_ALLOCATOR = new ObjectAllocator() {
0682:                    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
0683:                        return new Deflate(runtime, klass);
0684:                    }
0685:                };
0686:
0687:                public static IRubyObject s_deflate(IRubyObject recv,
0688:                        IRubyObject[] args) throws Exception {
0689:                    args = Arity.scanArgs(recv.getRuntime(), args, 1, 1);
0690:                    int level = -1;
0691:                    if (!args[1].isNil()) {
0692:                        level = RubyNumeric.fix2int(args[1]);
0693:                    }
0694:                    return ZlibDeflate.s_deflate(recv, args[0]
0695:                            .convertToString().getByteList(), level);
0696:                }
0697:
0698:                public Deflate(Ruby runtime, RubyClass type) {
0699:                    super (runtime, type);
0700:                }
0701:
0702:                private ZlibDeflate defl;
0703:
0704:                public IRubyObject _initialize(IRubyObject[] args)
0705:                        throws Exception {
0706:                    args = Arity.scanArgs(getRuntime(), args, 0, 4);
0707:                    int level = -1;
0708:                    int window_bits = 15;
0709:                    int memlevel = 8;
0710:                    int strategy = 0;
0711:                    if (!args[0].isNil()) {
0712:                        level = RubyNumeric.fix2int(args[0]);
0713:                    }
0714:                    if (!args[1].isNil()) {
0715:                        window_bits = RubyNumeric.fix2int(args[1]);
0716:                    }
0717:                    if (!args[2].isNil()) {
0718:                        memlevel = RubyNumeric.fix2int(args[2]);
0719:                    }
0720:                    if (!args[3].isNil()) {
0721:                        strategy = RubyNumeric.fix2int(args[3]);
0722:                    }
0723:                    defl = new ZlibDeflate(this , level, window_bits, memlevel,
0724:                            strategy);
0725:                    return this ;
0726:                }
0727:
0728:                public IRubyObject append(IRubyObject arg) throws Exception {
0729:                    defl.append(arg);
0730:                    return this ;
0731:                }
0732:
0733:                public IRubyObject params(IRubyObject level,
0734:                        IRubyObject strategy) {
0735:                    defl.params(RubyNumeric.fix2int(level), RubyNumeric
0736:                            .fix2int(strategy));
0737:                    return getRuntime().getNil();
0738:                }
0739:
0740:                public IRubyObject set_dictionary(IRubyObject arg)
0741:                        throws Exception {
0742:                    return defl.set_dictionary(arg);
0743:                }
0744:
0745:                public IRubyObject flush(IRubyObject[] args) throws Exception {
0746:                    int flush = 2; // SYNC_FLUSH
0747:                    if (Arity.checkArgumentCount(getRuntime(), args, 0, 1) == 1) {
0748:                        if (!args[0].isNil()) {
0749:                            flush = RubyNumeric.fix2int(args[0]);
0750:                        }
0751:                    }
0752:                    return defl.flush(flush);
0753:                }
0754:
0755:                public IRubyObject deflate(IRubyObject[] args) throws Exception {
0756:                    args = Arity.scanArgs(getRuntime(), args, 1, 1);
0757:                    int flush = 0; // NO_FLUSH
0758:                    if (!args[1].isNil()) {
0759:                        flush = RubyNumeric.fix2int(args[1]);
0760:                    }
0761:                    return defl.deflate(
0762:                            args[0].convertToString().getByteList(), flush);
0763:                }
0764:
0765:                protected int internalTotalOut() {
0766:                    return defl.getDeflater().getTotalOut();
0767:                }
0768:
0769:                protected boolean internalStreamEndP() {
0770:                    return defl.getDeflater().finished();
0771:                }
0772:
0773:                protected void internalEnd() {
0774:                    defl.getDeflater().end();
0775:                }
0776:
0777:                protected void internalReset() {
0778:                    defl.getDeflater().reset();
0779:                }
0780:
0781:                protected int internalAdler() {
0782:                    return defl.getDeflater().getAdler();
0783:                }
0784:
0785:                protected IRubyObject internalFinish() throws Exception {
0786:                    return defl.finish();
0787:                }
0788:
0789:                protected int internalTotalIn() {
0790:                    return defl.getDeflater().getTotalIn();
0791:                }
0792:
0793:                protected void internalClose() {
0794:                    defl.close();
0795:                }
0796:            }
0797:
0798:            public static class RubyGzipFile extends RubyObject {
0799:                public static IRubyObject wrap(IRubyObject recv,
0800:                        RubyGzipFile io, IRubyObject proc, Block unusedBlock)
0801:                        throws IOException {
0802:                    if (!proc.isNil()) {
0803:                        try {
0804:                            ((RubyProc) proc).call(new IRubyObject[] { io });
0805:                        } finally {
0806:                            if (!io.isClosed()) {
0807:                                io.close();
0808:                            }
0809:                        }
0810:                        return recv.getRuntime().getNil();
0811:                    }
0812:
0813:                    return io;
0814:                }
0815:
0816:                protected static ObjectAllocator GZIPFILE_ALLOCATOR = new ObjectAllocator() {
0817:                    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
0818:                        return new RubyGzipFile(runtime, klass);
0819:                    }
0820:                };
0821:
0822:                public static RubyGzipFile newInstance(IRubyObject recv,
0823:                        Block block) {
0824:                    RubyClass klass = (RubyClass) recv;
0825:
0826:                    RubyGzipFile result = (RubyGzipFile) klass.allocate();
0827:
0828:                    result.callInit(new IRubyObject[0], block);
0829:
0830:                    return result;
0831:                }
0832:
0833:                protected boolean closed = false;
0834:                protected boolean finished = false;
0835:                private int os_code = 255;
0836:                private int level = -1;
0837:                private String orig_name;
0838:                private String comment;
0839:                protected IRubyObject realIo;
0840:                private IRubyObject mtime;
0841:
0842:                public RubyGzipFile(Ruby runtime, RubyClass type) {
0843:                    super (runtime, type);
0844:                    mtime = runtime.getNil();
0845:                }
0846:
0847:                public IRubyObject os_code() {
0848:                    return getRuntime().newFixnum(os_code);
0849:                }
0850:
0851:                public IRubyObject closed_p() {
0852:                    return closed ? getRuntime().getTrue() : getRuntime()
0853:                            .getFalse();
0854:                }
0855:
0856:                protected boolean isClosed() {
0857:                    return closed;
0858:                }
0859:
0860:                public IRubyObject orig_name() {
0861:                    return orig_name == null ? getRuntime().getNil()
0862:                            : getRuntime().newString(orig_name);
0863:                }
0864:
0865:                public Object to_io() {
0866:                    return realIo;
0867:                }
0868:
0869:                public IRubyObject comment() {
0870:                    return comment == null ? getRuntime().getNil()
0871:                            : getRuntime().newString(comment);
0872:                }
0873:
0874:                public IRubyObject crc() {
0875:                    return RubyFixnum.zero(getRuntime());
0876:                }
0877:
0878:                public IRubyObject mtime() {
0879:                    return mtime;
0880:                }
0881:
0882:                public IRubyObject sync() {
0883:                    return getRuntime().getNil();
0884:                }
0885:
0886:                public IRubyObject finish() throws IOException {
0887:                    if (!finished) {
0888:                        //io.finish();
0889:                    }
0890:                    finished = true;
0891:                    return realIo;
0892:                }
0893:
0894:                public IRubyObject close() throws IOException {
0895:                    return null;
0896:                }
0897:
0898:                public IRubyObject level() {
0899:                    return getRuntime().newFixnum(level);
0900:                }
0901:
0902:                public IRubyObject set_sync(IRubyObject ignored) {
0903:                    return getRuntime().getNil();
0904:                }
0905:            }
0906:
0907:            public static class RubyGzipReader extends RubyGzipFile {
0908:                protected static ObjectAllocator GZIPREADER_ALLOCATOR = new ObjectAllocator() {
0909:                    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
0910:                        return new RubyGzipReader(runtime, klass);
0911:                    }
0912:                };
0913:
0914:                private static RubyGzipReader newInstance(IRubyObject recv,
0915:                        IRubyObject[] args, Block block) {
0916:                    RubyClass klass = (RubyClass) recv;
0917:                    RubyGzipReader result = (RubyGzipReader) klass.allocate();
0918:                    result.callInit(args, block);
0919:                    return result;
0920:                }
0921:
0922:                public static IRubyObject open(IRubyObject recv,
0923:                        RubyString filename, Block block) throws IOException {
0924:                    Ruby runtime = recv.getRuntime();
0925:                    IRubyObject proc = block.isGiven() ? runtime.newProc(false,
0926:                            block) : runtime.getNil();
0927:                    RubyGzipReader io = newInstance(
0928:                            recv,
0929:                            new IRubyObject[] { runtime.getClass("File")
0930:                                    .callMethod(
0931:                                            runtime.getCurrentContext(),
0932:                                            "open",
0933:                                            new IRubyObject[] { filename,
0934:                                                    runtime.newString("rb") }) },
0935:                            block);
0936:
0937:                    return RubyGzipFile.wrap(recv, io, proc, null);
0938:                }
0939:
0940:                public RubyGzipReader(Ruby runtime, RubyClass type) {
0941:                    super (runtime, type);
0942:                }
0943:
0944:                private int line;
0945:                private InputStream io;
0946:
0947:                public IRubyObject initialize(IRubyObject io, Block unusedBlock) {
0948:                    realIo = io;
0949:                    try {
0950:                        this .io = new GZIPInputStream(new IOInputStream(io));
0951:                    } catch (IOException e) {
0952:                        Ruby runtime = io.getRuntime();
0953:                        RubyClass errorClass = runtime.getModule("Zlib")
0954:                                .getClass("GzipReader").getClass("Error");
0955:                        throw new RaiseException(RubyException.newException(
0956:                                runtime, errorClass, e.getMessage()));
0957:                    }
0958:
0959:                    line = 1;
0960:
0961:                    return this ;
0962:                }
0963:
0964:                public IRubyObject rewind() {
0965:                    return getRuntime().getNil();
0966:                }
0967:
0968:                public IRubyObject lineno() {
0969:                    return getRuntime().newFixnum(line);
0970:                }
0971:
0972:                public IRubyObject readline() throws IOException {
0973:                    IRubyObject dst = gets(new IRubyObject[0]);
0974:                    if (dst.isNil()) {
0975:                        throw getRuntime().newEOFError();
0976:                    }
0977:                    return dst;
0978:                }
0979:
0980:                public IRubyObject internalGets(IRubyObject[] args)
0981:                        throws IOException {
0982:                    String sep = ((RubyString) getRuntime()
0983:                            .getGlobalVariables().get("$/")).getValue()
0984:                            .toString();
0985:                    if (args.length > 0) {
0986:                        sep = args[0].toString();
0987:                    }
0988:                    return internalSepGets(sep);
0989:                }
0990:
0991:                private IRubyObject internalSepGets(String sep)
0992:                        throws IOException {
0993:                    StringBuffer result = new StringBuffer();
0994:                    char ce = (char) io.read();
0995:                    while (ce != -1 && sep.indexOf(ce) == -1) {
0996:                        result.append((char) ce);
0997:                        ce = (char) io.read();
0998:                    }
0999:                    line++;
1000:                    return getRuntime()
1001:                            .newString(result.append(sep).toString());
1002:                }
1003:
1004:                public IRubyObject gets(IRubyObject[] args) throws IOException {
1005:                    IRubyObject result = internalGets(args);
1006:                    if (!result.isNil()) {
1007:                        getRuntime().getCurrentContext().setLastline(result);
1008:                    }
1009:                    return result;
1010:                }
1011:
1012:                private final static int BUFF_SIZE = 4096;
1013:
1014:                public IRubyObject read(IRubyObject[] args) throws IOException {
1015:                    if (args.length == 0 || args[0].isNil()) {
1016:                        ByteList val = new ByteList(10);
1017:                        byte[] buffer = new byte[BUFF_SIZE];
1018:                        int read = io.read(buffer);
1019:                        while (read != -1) {
1020:                            val.append(buffer, 0, read);
1021:                            read = io.read(buffer);
1022:                        }
1023:                        return RubyString.newString(getRuntime(), val);
1024:                    }
1025:
1026:                    int len = RubyNumeric.fix2int(args[0]);
1027:                    if (len < 0) {
1028:                        throw getRuntime().newArgumentError(
1029:                                "negative length " + len + " given");
1030:                    } else if (len > 0) {
1031:                        byte[] buffer = new byte[len];
1032:                        int toRead = len;
1033:                        int offset = 0;
1034:                        int read = 0;
1035:                        while (toRead > 0) {
1036:                            read = io.read(buffer, offset, toRead);
1037:                            if (read == -1) {
1038:                                break;
1039:                            }
1040:                            toRead -= read;
1041:                            offset += read;
1042:                        } // hmm...
1043:                        return RubyString.newString(getRuntime(), new ByteList(
1044:                                buffer, 0, len - toRead, false));
1045:                    }
1046:
1047:                    return getRuntime().newString("");
1048:                }
1049:
1050:                public IRubyObject set_lineno(RubyNumeric lineArg) {
1051:                    line = RubyNumeric.fix2int(lineArg);
1052:                    return lineArg;
1053:                }
1054:
1055:                public IRubyObject pos() {
1056:                    return RubyFixnum.zero(getRuntime());
1057:                }
1058:
1059:                public IRubyObject readchar() throws IOException {
1060:                    int value = io.read();
1061:                    if (value == -1) {
1062:                        throw getRuntime().newEOFError();
1063:                    }
1064:                    return getRuntime().newFixnum(value);
1065:                }
1066:
1067:                public IRubyObject getc() throws IOException {
1068:                    int value = io.read();
1069:                    return value == -1 ? getRuntime().getNil() : getRuntime()
1070:                            .newFixnum(value);
1071:                }
1072:
1073:                private boolean isEof() throws IOException {
1074:                    return ((GZIPInputStream) io).available() != 1;
1075:                }
1076:
1077:                public IRubyObject close() throws IOException {
1078:                    if (!closed) {
1079:                        io.close();
1080:                    }
1081:                    this .closed = true;
1082:                    return getRuntime().getNil();
1083:                }
1084:
1085:                public IRubyObject eof() throws IOException {
1086:                    return isEof() ? getRuntime().getTrue() : getRuntime()
1087:                            .getFalse();
1088:                }
1089:
1090:                public IRubyObject eof_p() throws IOException {
1091:                    return eof();
1092:                }
1093:
1094:                public IRubyObject unused() {
1095:                    return getRuntime().getNil();
1096:                }
1097:
1098:                public IRubyObject tell() {
1099:                    return getRuntime().getNil();
1100:                }
1101:
1102:                public IRubyObject each(IRubyObject[] args, Block block)
1103:                        throws IOException {
1104:                    String sep = ((RubyString) getRuntime()
1105:                            .getGlobalVariables().get("$/")).getValue()
1106:                            .toString();
1107:
1108:                    if (args.length > 0 && !args[0].isNil()) {
1109:                        sep = args[0].toString();
1110:                    }
1111:
1112:                    ThreadContext context = getRuntime().getCurrentContext();
1113:                    while (!isEof()) {
1114:                        block.yield(context, internalSepGets(sep));
1115:                    }
1116:
1117:                    return getRuntime().getNil();
1118:                }
1119:
1120:                public IRubyObject ungetc(RubyNumeric arg) {
1121:                    return getRuntime().getNil();
1122:                }
1123:
1124:                public IRubyObject readlines(IRubyObject[] args)
1125:                        throws IOException {
1126:                    List array = new ArrayList();
1127:
1128:                    if (args.length != 0 && args[0].isNil()) {
1129:                        array.add(read(new IRubyObject[0]));
1130:                    } else {
1131:                        String seperator = ((RubyString) getRuntime()
1132:                                .getGlobalVariables().get("$/")).getValue()
1133:                                .toString();
1134:                        if (args.length > 0) {
1135:                            seperator = args[0].toString();
1136:                        }
1137:                        while (!isEof()) {
1138:                            array.add(internalSepGets(seperator));
1139:                        }
1140:                    }
1141:                    return getRuntime().newArray(array);
1142:                }
1143:
1144:                public IRubyObject each_byte(Block block) throws IOException {
1145:                    int value = io.read();
1146:
1147:                    ThreadContext context = getRuntime().getCurrentContext();
1148:                    while (value != -1) {
1149:                        block.yield(context, getRuntime().newFixnum(value));
1150:                        value = io.read();
1151:                    }
1152:
1153:                    return getRuntime().getNil();
1154:                }
1155:            }
1156:
1157:            public static class RubyGzipWriter extends RubyGzipFile {
1158:                protected static ObjectAllocator GZIPWRITER_ALLOCATOR = new ObjectAllocator() {
1159:                    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
1160:                        return new RubyGzipWriter(runtime, klass);
1161:                    }
1162:                };
1163:
1164:                private static RubyGzipWriter newGzipWriter(IRubyObject recv,
1165:                        IRubyObject[] args, Block block) {
1166:                    RubyClass klass = (RubyClass) recv;
1167:
1168:                    RubyGzipWriter result = (RubyGzipWriter) klass.allocate();
1169:                    result.callInit(args, block);
1170:                    return result;
1171:                }
1172:
1173:                public static IRubyObject open(IRubyObject recv,
1174:                        IRubyObject[] args, Block block) throws IOException {
1175:                    Ruby runtime = recv.getRuntime();
1176:                    IRubyObject level = runtime.getNil();
1177:                    IRubyObject strategy = runtime.getNil();
1178:
1179:                    if (args.length > 1) {
1180:                        level = args[1];
1181:                        if (args.length > 2)
1182:                            strategy = args[2];
1183:                    }
1184:
1185:                    IRubyObject proc = block.isGiven() ? runtime.newProc(false,
1186:                            block) : runtime.getNil();
1187:                    RubyGzipWriter io = newGzipWriter(recv, new IRubyObject[] {
1188:                            runtime.getClass("File").callMethod(
1189:                                    runtime.getCurrentContext(),
1190:                                    "open",
1191:                                    new IRubyObject[] { args[0],
1192:                                            runtime.newString("wb") }), level,
1193:                            strategy }, block);
1194:
1195:                    return RubyGzipFile.wrap(recv, io, proc, null);
1196:                }
1197:
1198:                public RubyGzipWriter(Ruby runtime, RubyClass type) {
1199:                    super (runtime, type);
1200:                }
1201:
1202:                private GZIPOutputStream io;
1203:
1204:                public IRubyObject initialize2(IRubyObject[] args,
1205:                        Block unusedBlock) throws IOException {
1206:                    realIo = (RubyObject) args[0];
1207:                    this .io = new GZIPOutputStream(new IOOutputStream(args[0]));
1208:
1209:                    return this ;
1210:                }
1211:
1212:                public IRubyObject close() throws IOException {
1213:                    if (!closed) {
1214:                        io.close();
1215:                    }
1216:                    this .closed = true;
1217:
1218:                    return getRuntime().getNil();
1219:                }
1220:
1221:                public IRubyObject append(IRubyObject p1) throws IOException {
1222:                    this .write(p1);
1223:                    return this ;
1224:                }
1225:
1226:                public IRubyObject printf(IRubyObject[] args)
1227:                        throws IOException {
1228:                    write(RubyKernel.sprintf(this , args));
1229:                    return getRuntime().getNil();
1230:                }
1231:
1232:                public IRubyObject print(IRubyObject[] args) throws IOException {
1233:                    if (args.length != 0) {
1234:                        for (int i = 0, j = args.length; i < j; i++) {
1235:                            write(args[i]);
1236:                        }
1237:                    }
1238:
1239:                    IRubyObject sep = getRuntime().getGlobalVariables().get(
1240:                            "$\\");
1241:                    if (!sep.isNil()) {
1242:                        write(sep);
1243:                    }
1244:
1245:                    return getRuntime().getNil();
1246:                }
1247:
1248:                public IRubyObject pos() {
1249:                    return getRuntime().getNil();
1250:                }
1251:
1252:                public IRubyObject set_orig_name(RubyString ignored) {
1253:                    return getRuntime().getNil();
1254:                }
1255:
1256:                public IRubyObject set_comment(RubyString ignored) {
1257:                    return getRuntime().getNil();
1258:                }
1259:
1260:                public IRubyObject putc(RubyNumeric p1) throws IOException {
1261:                    io.write(RubyNumeric.fix2int(p1));
1262:                    return p1;
1263:                }
1264:
1265:                public IRubyObject puts(IRubyObject[] args) throws IOException {
1266:                    RubyStringIO sio = (RubyStringIO) getRuntime().getClass(
1267:                            "StringIO").newInstance(new IRubyObject[0],
1268:                            Block.NULL_BLOCK);
1269:                    sio.puts(args);
1270:                    write(sio.string());
1271:
1272:                    return getRuntime().getNil();
1273:                }
1274:
1275:                public IRubyObject finish() throws IOException {
1276:                    if (!finished) {
1277:                        io.finish();
1278:                    }
1279:                    finished = true;
1280:                    return realIo;
1281:                }
1282:
1283:                public IRubyObject flush(IRubyObject[] args) throws IOException {
1284:                    if (args.length == 0 || args[0].isNil()
1285:                            || RubyNumeric.fix2int(args[0]) != 0) { // Zlib::NO_FLUSH
1286:                        io.flush();
1287:                    }
1288:                    return getRuntime().getNil();
1289:                }
1290:
1291:                public IRubyObject set_mtime(IRubyObject ignored) {
1292:                    return getRuntime().getNil();
1293:                }
1294:
1295:                public IRubyObject tell() {
1296:                    return getRuntime().getNil();
1297:                }
1298:
1299:                public IRubyObject write(IRubyObject p1) throws IOException {
1300:                    ByteList bytes = p1.convertToString().getByteList();
1301:                    io
1302:                            .write(bytes.unsafeBytes(), bytes.begin(), bytes
1303:                                    .length());
1304:                    return getRuntime().newFixnum(bytes.length());
1305:                }
1306:            }
1307:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.