const byte *FileInputSource::openStream() { if (stream != null) throw InputSourceException(StringBuffer("openStream(): source stream already opened: '")+baseLocation+"'"); int source = open(baseLocation->getChars(), O_BINARY); if (source == -1) throw InputSourceException(StringBuffer("Can't open file '")+baseLocation+"'"); struct stat st; fstat(source, &st); len = st.st_size; stream = new byte[len]; read(source, stream, len); close(source); return stream; };
JARInputSource::JARInputSource(const String *basePath, InputSource *base){ if (basePath == null) throw InputSourceException(StringBuffer("Can't create jar source")); // absolute jar uri int ex_idx = basePath->lastIndexOf('!'); if (ex_idx == -1) throw InputSourceException(StringBuffer("Bad jar uri format: ") + basePath); inJarLocation = new SString(basePath, ex_idx+1, -1); sharedIS = new SharedInputSource(&DString(basePath, 4, ex_idx-4), base); sharedIS->addref(); baseLocation = new SString(sharedIS->getLocation()); stream = null; len = 0; };
const byte *JARInputSource::openStream() { if (stream != null) throw InputSourceException(StringBuffer("openStream(): source stream already opened: '")+baseLocation+"'"); MemoryFile *mf = new MemoryFile; mf->stream = sharedIS->getStream(); mf->length = sharedIS->length(); zlib_filefunc_def zlib_ff; fill_mem_filefunc(&zlib_ff, mf); unzFile fid = unzOpen2(null, &zlib_ff); if (fid == 0) throw InputSourceException(StringBuffer("Can't locate file in JAR content: '")+inJarLocation+"'"); int ret = unzLocateFile(fid, inJarLocation->getChars(), 0); if (ret != UNZ_OK) throw InputSourceException(StringBuffer("Can't locate file in JAR content: '")+inJarLocation+"'"); unz_file_info file_info; ret = unzGetCurrentFileInfo(fid, &file_info, null, 0, null, 0, null, 0); if (ret != UNZ_OK) throw InputSourceException(StringBuffer("Can't retrieve current file in JAR content: '")+inJarLocation+"'"); len = file_info.uncompressed_size; stream = new byte[len]; ret = unzOpenCurrentFile(fid); if (ret != UNZ_OK) throw InputSourceException(StringBuffer("Can't open current file in JAR content: '")+inJarLocation+"'"); ret = unzReadCurrentFile(fid, stream, len); if (ret <= 0) throw InputSourceException(StringBuffer("Can't read current file in JAR content: '")+inJarLocation+"' ("+SString(ret)+")"); ret = unzCloseCurrentFile(fid); if (ret == UNZ_CRCERROR) throw InputSourceException(StringBuffer("Bad JAR file CRC")); ret = unzClose(fid); return stream; };
JARInputSource::JARInputSource(const String *basePath, InputSource *base){ if (basePath == null) throw InputSourceException(StringBuffer("Can't create jar source")); // absolute jar uri int ex_idx = basePath->lastIndexOf('!'); if (ex_idx == -1) throw InputSourceException(StringBuffer("Bad jar uri format: ") + basePath); inJarLocation = new SString(basePath, ex_idx+1, -1); DString ds_bp(basePath, 4, ex_idx-4); sharedIS = SharedInputSource::getInputSource(&ds_bp, base); StringBuffer str("jar:"); str.append(sharedIS->getLocation()); str.append(DString("!")); str.append(inJarLocation); baseLocation = new SString(&str); stream = null; len = 0; }
const byte *FileInputSource::openStream() { if (stream != null) throw InputSourceException(StringBuffer("openStream(): source stream already opened: '")+baseLocation+"'"); #ifdef _UNICODE int source = open(Wide2MB(baseLocation->getWChars()).c_str(), O_BINARY); #else int source = open(baseLocation->getChars(), O_BINARY); #endif if (source == -1) { fprintf(stderr, "failed to open %ls\n", baseLocation->getWChars()); throw InputSourceException(StringBuffer("Can't open file '")+baseLocation+"'"); } struct stat st; fstat(source, &st); len = st.st_size; stream = new byte[len]; memset(stream,0, sizeof(byte)*len); read(source, stream, len); close(source); return stream; };
JARInputSource::JARInputSource(const String *basePath, JARInputSource *base, bool faked){ // relative jar uri JARInputSource *parent = base; if (parent == null) throw InputSourceException(StringBuffer("Bad jar uri format: ") + basePath); sharedIS = parent->getShared(); sharedIS->addref(); inJarLocation = getAbsolutePath(parent->getInJarLocation(), basePath); StringBuffer str("jar:"); str.append(sharedIS->getLocation()); str.append(DString("!")); str.append(inJarLocation); baseLocation = new SString(&str); stream = null; len = 0; };
int FileInputSource::length() const{ if (stream == null) throw InputSourceException(DString("length(): stream is not yet opened")); return len; };
void FileInputSource::closeStream(){ if (stream == null) throw InputSourceException(StringBuffer("closeStream(): source stream is not yet opened")); delete[] stream; stream = null; };