mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
uca category support and other changes
This commit is contained in:
@@ -49,6 +49,12 @@ public class Hex_utl_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static byte[] Encode_bry(byte[] src) {
|
||||
int src_len = src.length;
|
||||
byte[] trg = new byte[src_len * 2];
|
||||
Encode_bry(src, trg);
|
||||
return trg;
|
||||
}
|
||||
public static void Encode_bry(byte[] src, byte[] trg) {
|
||||
int src_len = src.length, trg_len = trg.length;
|
||||
if (trg_len != src_len * 2) throw Err_.new_("hex", "trg.len must be src.len * 2", "src_len", src_len, "trg_len", trg_len);
|
||||
@@ -59,6 +65,14 @@ public class Hex_utl_ {
|
||||
trg[++trg_idx] = To_byte_lcase(0xf & src_byte);
|
||||
}
|
||||
}
|
||||
public static void Encode_bfr(Bry_bfr bfr, byte[] src) {
|
||||
int src_len = src.length;
|
||||
for (int src_idx = 0; src_idx < src_len; ++src_idx) {
|
||||
byte src_byte = src[src_idx];
|
||||
bfr.Add_byte(To_byte_lcase(0xf & src_byte >>> 4));
|
||||
bfr.Add_byte(To_byte_lcase(0xf & src_byte));
|
||||
}
|
||||
}
|
||||
public static String To_str(int val, int pad) {
|
||||
char[] ary = new char[8]; int idx = 8; // 8 is max len of hexString; (2^4 * 8); EX: int.MaxValue = 7FFFFFFF
|
||||
do {
|
||||
|
||||
@@ -190,7 +190,7 @@ public class IoEngine_memory extends IoEngine_base {
|
||||
return Io_stream_rdr_.Noop;
|
||||
}
|
||||
byte[] bry = Bry_.new_u8(FetchFil(Io_url_.mem_fil_(xrg.Src())).Text());
|
||||
return Io_stream_rdr_.mem_(bry);
|
||||
return Io_stream_rdr_.New__mem(bry);
|
||||
}
|
||||
|
||||
IoItmHash dirs = IoItmHash.new_();
|
||||
|
||||
@@ -638,7 +638,7 @@ class Io_stream_rdr_http implements Io_stream_rdr {
|
||||
public Io_stream_rdr_http(IoEngine_xrg_downloadFil xrg) {
|
||||
this.xrg = xrg;
|
||||
} private IoEngine_xrg_downloadFil xrg;
|
||||
public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public byte Tid() {return Io_stream_tid_.Tid__raw;}
|
||||
public boolean Exists() {return exists;} private boolean exists = false;
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {url = v; return this;} private Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = IoItmFil.Size_invalid; // NOTE: must default size to -1; DATE:2014-06-21
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
public class Io_stream_ { // SERIALIZED
|
||||
public static final byte Tid_null = 0, Tid_raw = 1, Tid_zip = 2, Tid_gzip = 3, Tid_bzip2 = 4;
|
||||
public static final String Ext_zip = ".zip", Ext_gz = ".gz", Ext_bz2 = ".bz2";
|
||||
|
||||
public static String Obsolete_to_str(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_.Tid_raw : return ".xdat";
|
||||
case Io_stream_.Tid_zip : return ".zip";
|
||||
case Io_stream_.Tid_gzip : return ".gz";
|
||||
case Io_stream_.Tid_bzip2 : return ".bz2";
|
||||
default : throw Err_.new_unhandled(v);
|
||||
}
|
||||
}
|
||||
public static byte Obsolete_to_tid(String v) {
|
||||
if (String_.Eq(v, ".xdat")) return Io_stream_.Tid_raw;
|
||||
else if (String_.Eq(v, ".zip")) return Io_stream_.Tid_zip;
|
||||
else if (String_.Eq(v, ".gz")) return Io_stream_.Tid_gzip;
|
||||
else if (String_.Eq(v, ".bz2")) return Io_stream_.Tid_bzip2;
|
||||
else throw Err_.new_unhandled(v);
|
||||
}
|
||||
public static String To_str(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_.Tid_raw : return "raw";
|
||||
case Io_stream_.Tid_zip : return "zip";
|
||||
case Io_stream_.Tid_gzip : return "gzip";
|
||||
case Io_stream_.Tid_bzip2 : return "bzip2";
|
||||
default : throw Err_.new_unhandled(v);
|
||||
}
|
||||
}
|
||||
public static byte To_tid(String v) {
|
||||
if (String_.Eq(v, "raw")) return Io_stream_.Tid_raw;
|
||||
else if (String_.Eq(v, "zip")) return Io_stream_.Tid_zip;
|
||||
else if (String_.Eq(v, "gzip")) return Io_stream_.Tid_gzip;
|
||||
else if (String_.Eq(v, "bzip2")) return Io_stream_.Tid_bzip2;
|
||||
else throw Err_.new_unhandled(v);
|
||||
}
|
||||
}
|
||||
@@ -17,14 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
public interface Io_stream_rdr extends Rls_able {
|
||||
byte Tid();
|
||||
boolean Exists();
|
||||
Io_url Url(); Io_stream_rdr Url_(Io_url v);
|
||||
long Len(); Io_stream_rdr Len_(long v);
|
||||
Io_stream_rdr Open();
|
||||
void Open_mem(byte[] v);
|
||||
Object Under();
|
||||
byte Tid();
|
||||
boolean Exists();
|
||||
Io_url Url(); Io_stream_rdr Url_(Io_url v);
|
||||
long Len(); Io_stream_rdr Len_(long v);
|
||||
Io_stream_rdr Open();
|
||||
void Open_mem(byte[] v);
|
||||
Object Under();
|
||||
|
||||
int Read(byte[] bry, int bgn, int len);
|
||||
long Skip(long len);
|
||||
int Read(byte[] bry, int bgn, int len);
|
||||
long Skip(long len);
|
||||
}
|
||||
|
||||
@@ -16,39 +16,43 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
import gplx.core.ios.streams.rdrs.*;
|
||||
public class Io_stream_rdr_ {
|
||||
public static Io_stream_rdr file_(Io_url url) {return new Io_stream_rdr_file().Url_(url);}
|
||||
public static Io_stream_rdr file_(java.io.InputStream strm) {return new Io_stream_rdr_file().Under_(strm);}
|
||||
public static Io_stream_rdr zip_(Io_url url) {return new Io_stream_rdr_zip().Url_(url);}
|
||||
public static Io_stream_rdr gzip_(Io_url url) {return new Io_stream_rdr_gzip().Url_(url);}
|
||||
public static Io_stream_rdr bzip2_(Io_url url) {return new Io_stream_rdr_bzip2().Url_(url);}
|
||||
public static Io_stream_rdr new_by_url_(Io_url url) {
|
||||
public static final int Read_done = -1, Read_done_compare = 1;
|
||||
public static final Io_stream_rdr Noop = new Io_stream_rdr__noop();
|
||||
public static Io_stream_rdr New__raw(Io_url url) {return new Io_stream_rdr__raw().Url_(url);}
|
||||
public static Io_stream_rdr New__raw(java.io.InputStream strm) {return new Io_stream_rdr__raw().Under_(strm);}
|
||||
private static Io_stream_rdr New__zip(Io_url url) {return new Io_stream_rdr__zip().Url_(url);}
|
||||
private static Io_stream_rdr New__gzip(Io_url url) {return new Io_stream_rdr__gzip().Url_(url);}
|
||||
public static Io_stream_rdr New__bzip2(Io_url url) {return new Io_stream_rdr__bzip2().Url_(url);}
|
||||
public static Io_stream_rdr New__mem(String v) {return New__mem(Bry_.new_u8(v));}
|
||||
public static Io_stream_rdr New__mem(byte[] v) {
|
||||
Io_stream_rdr rv = new Io_stream_rdr__adp(New__mem_as_stream(v));
|
||||
rv.Len_(v.length);
|
||||
return rv;
|
||||
}
|
||||
public static java.io.InputStream New__mem_as_stream(byte[] v) {
|
||||
return new java.io.ByteArrayInputStream(v);
|
||||
}
|
||||
public static Io_stream_rdr New_by_url(Io_url url) {
|
||||
String ext = url.Ext();
|
||||
if (String_.Eq(ext, Io_stream_.Ext_zip)) return gplx.core.ios.streams.Io_stream_rdr_.zip_(url);
|
||||
else if (String_.Eq(ext, Io_stream_.Ext_gz)) return gplx.core.ios.streams.Io_stream_rdr_.gzip_(url);
|
||||
else if (String_.Eq(ext, Io_stream_.Ext_bz2)) return gplx.core.ios.streams.Io_stream_rdr_.bzip2_(url);
|
||||
else return gplx.core.ios.streams.Io_stream_rdr_.file_(url);
|
||||
if (String_.Eq(ext, Io_stream_tid_.Ext__zip)) return Io_stream_rdr_.New__zip(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__gz)) return Io_stream_rdr_.New__gzip(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__bz2)) return Io_stream_rdr_.New__bzip2(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__xz)) return new Io_stream_rdr__xz().Url_(url);
|
||||
else return Io_stream_rdr_.New__raw(url);
|
||||
}
|
||||
public static Io_stream_rdr new_by_tid_(byte tid) {
|
||||
public static Io_stream_rdr New_by_tid(byte tid) {
|
||||
switch (tid) {
|
||||
case Io_stream_.Tid_raw: return new Io_stream_rdr_file();
|
||||
case Io_stream_.Tid_zip: return new Io_stream_rdr_zip();
|
||||
case Io_stream_.Tid_gzip: return new Io_stream_rdr_gzip();
|
||||
case Io_stream_.Tid_bzip2: return new Io_stream_rdr_bzip2();
|
||||
default: throw Err_.new_unhandled(tid);
|
||||
case Io_stream_tid_.Tid__raw: return new Io_stream_rdr__raw();
|
||||
case Io_stream_tid_.Tid__zip: return new Io_stream_rdr__zip();
|
||||
case Io_stream_tid_.Tid__gzip: return new Io_stream_rdr__gzip();
|
||||
case Io_stream_tid_.Tid__bzip2: return new Io_stream_rdr__bzip2();
|
||||
case Io_stream_tid_.Tid__xz: return new Io_stream_rdr__xz();
|
||||
default: throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
}
|
||||
public static byte[] Load_all(Io_url url) {
|
||||
Io_stream_rdr rdr = new_by_url_(url);
|
||||
Bry_bfr rv = Bry_bfr_.New();
|
||||
try {
|
||||
rdr.Open();
|
||||
return Load_all_as_bry(rv, rdr);
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
public static String Load_all_as_str(Io_stream_rdr rdr) {return String_.new_u8(Load_all_as_bry(rdr));}
|
||||
public static byte[] Load_all_as_bry(Io_stream_rdr rdr) {return Load_all_as_bry(Bry_bfr_.New(), rdr);}
|
||||
public static String Load_all_as_str(Io_stream_rdr rdr) {return String_.new_u8(Load_all_as_bry(Bry_bfr_.New(), rdr));}
|
||||
public static byte[] Load_all_as_bry(Bry_bfr rv, Io_stream_rdr rdr) {
|
||||
Load_all_to_bfr(rv, rdr);
|
||||
return rv.To_bry_and_clear();
|
||||
@@ -63,24 +67,7 @@ public class Io_stream_rdr_ {
|
||||
}
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
public static final Io_stream_rdr Noop = new Io_stream_rdr_noop();
|
||||
public static Io_stream_rdr mem_(String v) {return mem_(Bry_.new_u8(v));}
|
||||
public static Io_stream_rdr mem_(byte[] v) {
|
||||
Io_stream_rdr rv = new Io_stream_rdr_adp(Stream_new_mem(v));
|
||||
rv.Len_(v.length);
|
||||
return rv;
|
||||
}
|
||||
public static java.io.InputStream Stream_new_mem(byte[] v) {
|
||||
return new java.io.ByteArrayInputStream(v);
|
||||
}
|
||||
public static boolean Stream_close(java.io.InputStream stream) {
|
||||
try {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
return true;
|
||||
} catch (Exception e) {Err_.Noop(e); return false;}
|
||||
}
|
||||
public static int Stream_read_by_parts(java.io.InputStream stream, int part_len, byte[] bry, int bgn, int len) {
|
||||
public static int Read_by_parts(java.io.InputStream stream, int part_len, byte[] bry, int bgn, int len) {
|
||||
/*
|
||||
NOTE: BZip2CompressorInputStream will fail if large len is used
|
||||
Instead, make smaller requests and fill bry
|
||||
@@ -106,163 +93,10 @@ public class Io_stream_rdr_ {
|
||||
throw Err_.new_exc(exc, "io", "read failed", "bgn", bgn, "len", len);
|
||||
}
|
||||
}
|
||||
public static final int Read_done = -1;
|
||||
public static final int Read_done_compare = 1;
|
||||
}
|
||||
class Io_stream_rdr_noop implements Io_stream_rdr {
|
||||
public Object Under() {return null;}
|
||||
public byte Tid() {return Io_stream_.Tid_null;}
|
||||
public boolean Exists() {return false;}
|
||||
public Io_url Url() {return Io_url_.Empty;} public Io_stream_rdr Url_(Io_url v) {return this;}
|
||||
public long Len() {return Io_mgr.Len_null;} public Io_stream_rdr Len_(long v) {return this;}
|
||||
public void Open_mem(byte[] v) {}
|
||||
public Io_stream_rdr Open() {return this;}
|
||||
public int Read(byte[] bry, int bgn, int len) {return Io_stream_rdr_.Read_done;}
|
||||
public long Skip(long len) {return Io_stream_rdr_.Read_done;}
|
||||
public void Rls() {}
|
||||
}
|
||||
class Io_stream_rdr_adp implements Io_stream_rdr {
|
||||
private java.io.InputStream strm;
|
||||
public Io_stream_rdr_adp(java.io.InputStream strm) {this.strm = strm;}
|
||||
public Object Under() {return strm;}
|
||||
public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public boolean Exists() {return len > 0;}
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {this.url = v; return this;} private Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = Io_mgr.Len_null;
|
||||
public void Open_mem(byte[] v) {}
|
||||
public Io_stream_rdr Open() {return this;}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {return strm.read(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return strm.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {strm.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
}
|
||||
}
|
||||
abstract class Io_stream_rdr_base implements Io_stream_rdr {
|
||||
public abstract byte Tid();
|
||||
public boolean Exists() {return this.Len() > 0;}
|
||||
public Object Under() {return stream;} public Io_stream_rdr Under_(java.io.InputStream v) {this.stream = v; return this;} protected java.io.InputStream stream;
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {this.url = v; return this;} protected Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = Io_mgr.Len_null;
|
||||
public void Open_mem(byte[] v) {
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(v));
|
||||
}
|
||||
public Io_stream_rdr Open() {
|
||||
try {stream = Wrap_stream(new java.io.FileInputStream(url.Xto_api()));}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {return stream.read(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return stream.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {stream.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
}
|
||||
public abstract java.io.InputStream Wrap_stream(java.io.InputStream stream);
|
||||
}
|
||||
class Io_stream_rdr_file extends Io_stream_rdr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public Io_stream_rdr Open() {
|
||||
public static boolean Close(java.io.InputStream stream) {
|
||||
try {
|
||||
if (!Io_mgr.Instance.Exists(url))
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(Bry_.Empty));
|
||||
else {
|
||||
if (url.Info().EngineKey() == IoEngine_.MemKey)
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(Io_mgr.Instance.LoadFilBry(url.Xto_api())));
|
||||
else
|
||||
stream = Wrap_stream(new java.io.FileInputStream(url.Xto_api()));
|
||||
}
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {return stream;}
|
||||
}
|
||||
class Io_stream_rdr_zip implements Io_stream_rdr {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_zip;}
|
||||
public boolean Exists() {return this.Len() > 0;}
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {this.url = v; return this;} Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = Io_mgr.Len_null;
|
||||
public Object Under() {return zip_stream;} private java.util.zip.ZipInputStream zip_stream;
|
||||
public void Src_bfr_(Bry_bfr v) {this.src_bfr = v;} Bry_bfr src_bfr;
|
||||
public void Open_mem(byte[] v) {
|
||||
Wrap_stream(new java.io.ByteArrayInputStream(v));
|
||||
}
|
||||
public Io_stream_rdr Open() {
|
||||
try {Wrap_stream(new java.io.FileInputStream(url.Xto_api()));}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
void Wrap_stream(java.io.InputStream input_stream) {zip_stream = new java.util.zip.ZipInputStream(input_stream);}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {
|
||||
while (true){
|
||||
int read = zip_stream.read(bry, bgn, len);
|
||||
if (read == Io_stream_rdr_.Read_done) {
|
||||
if (zip_stream.getNextEntry() == null)
|
||||
return Io_stream_rdr_.Read_done;
|
||||
}
|
||||
else
|
||||
return read;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return zip_stream.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {zip_stream.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
if (stream != null) stream.close();
|
||||
return true;
|
||||
} catch (Exception e) {Err_.Noop(e); return false;}
|
||||
}
|
||||
}
|
||||
class Io_stream_rdr_gzip extends Io_stream_rdr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_gzip;}
|
||||
@Override public int Read(byte[] bry, int bgn, int len) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
int total_read = 0;
|
||||
while (true) { // NOTE: the gz stream reads partially; (request 100; only get back 10); keep reading until entire bfr is full or -1
|
||||
int read = stream.read(bry, bgn, len);
|
||||
if (read == Io_stream_rdr_.Read_done) break;
|
||||
total_read += read;
|
||||
if (total_read >= len) break; // entire bfr full; stop
|
||||
bgn += read; // increase bgn by amount read
|
||||
len -= read; // decrease len by amount read
|
||||
}
|
||||
return total_read == 0 ? Io_stream_rdr_.Read_done : total_read; // gzip seems to allow 0 bytes read (bz2 and zip return -1 instead); normalize return to -1;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {
|
||||
try {return new java.util.zip.GZIPInputStream(stream);}
|
||||
catch (Exception exc) {throw Err_.new_wo_type("failed to open gz stream");}
|
||||
}
|
||||
}
|
||||
class Io_stream_rdr_bzip2 extends Io_stream_rdr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_bzip2;}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {
|
||||
try {return new org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream(stream, true);}
|
||||
catch (Exception exc) {throw Err_.new_wo_type("failed to open bzip2 stream");}
|
||||
}
|
||||
@Override public int Read(byte[] bry, int bgn, int len) {
|
||||
return Io_stream_rdr_.Stream_read_by_parts(stream, Read_len, bry, bgn, len);
|
||||
}
|
||||
private static final int Read_len = Io_mgr.Len_mb * 128;
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
import org.junit.*;
|
||||
public class Io_stream_rdr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Io_stream_rdr_fxt fxt = new Io_stream_rdr_fxt();
|
||||
public class Io_stream_rdr__tst {
|
||||
@Before public void init() {fxt.Clear();} private Io_stream_rdr__fxt fxt = new Io_stream_rdr__fxt();
|
||||
@After public void term() {fxt.Rls();}
|
||||
@Test public void Bz2_read() {
|
||||
fxt .Init_stream("abcd") // read everything at once
|
||||
@@ -30,27 +30,27 @@ public class Io_stream_rdr_tst {
|
||||
;
|
||||
}
|
||||
}
|
||||
class Io_stream_rdr_fxt {
|
||||
class Io_stream_rdr__fxt {
|
||||
private java.io.InputStream stream;
|
||||
private int stream_bry_len;
|
||||
public void Clear() {
|
||||
expd_bytes_read = Int_.Min_value;
|
||||
}
|
||||
public Io_stream_rdr_fxt Expd_bytes_read(int v) {expd_bytes_read = v; return this;} private int expd_bytes_read = Int_.Min_value;
|
||||
public Io_stream_rdr_fxt Init_stream(String v) {
|
||||
public Io_stream_rdr__fxt Expd_bytes_read(int v) {expd_bytes_read = v; return this;} private int expd_bytes_read = Int_.Min_value;
|
||||
public Io_stream_rdr__fxt Init_stream(String v) {
|
||||
byte[] stream_bry = Bry_.new_a7(v);
|
||||
stream_bry_len = stream_bry.length;
|
||||
stream = Io_stream_rdr_.Stream_new_mem(stream_bry);
|
||||
stream = Io_stream_rdr_.New__mem_as_stream(stream_bry);
|
||||
return this;
|
||||
}
|
||||
public Io_stream_rdr_fxt Test_read(int bgn, int len, String expd_str) {
|
||||
public Io_stream_rdr__fxt Test_read(int bgn, int len, String expd_str) {
|
||||
byte[] bfr = new byte[stream_bry_len]; // allocate whole stream; may not use it all
|
||||
int actl_bytes_read = Io_stream_rdr_.Stream_read_by_parts(stream, 8, bfr, bgn, len);
|
||||
int actl_bytes_read = Io_stream_rdr_.Read_by_parts(stream, 8, bfr, bgn, len);
|
||||
Tfds.Eq(expd_bytes_read, actl_bytes_read, "bytes_read");
|
||||
Tfds.Eq(expd_str, String_.new_u8(bfr, bgn, bgn + actl_bytes_read), "str");
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
Io_stream_rdr_.Stream_close(stream);
|
||||
Io_stream_rdr_.Close(stream);
|
||||
}
|
||||
}
|
||||
58
100_core/src/gplx/core/ios/streams/Io_stream_tid_.java
Normal file
58
100_core/src/gplx/core/ios/streams/Io_stream_tid_.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
public class Io_stream_tid_ {
|
||||
public static final byte Tid__null = 0, Tid__raw = 1, Tid__zip = 2, Tid__gzip = 3, Tid__bzip2 = 4, Tid__xz = 5; // SERIALIZED:xo.text_db;xo.html_db
|
||||
public static final String Ext__zip = ".zip", Ext__gz = ".gz", Ext__bz2 = ".bz2", Ext__xz = ".xz";
|
||||
private static final String Key__raw = "raw", Key__zip = "zip", Key__gzip = "gzip", Key__bzip2 = "bzip2", Key__xz = "xz";
|
||||
|
||||
public static String To_key(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_tid_.Tid__raw : return Key__raw;
|
||||
case Io_stream_tid_.Tid__zip : return Key__zip;
|
||||
case Io_stream_tid_.Tid__gzip : return Key__gzip;
|
||||
case Io_stream_tid_.Tid__bzip2 : return Key__bzip2;
|
||||
case Io_stream_tid_.Tid__xz : return Key__xz;
|
||||
default : throw Err_.new_unhandled_default(v);
|
||||
}
|
||||
}
|
||||
public static byte To_tid(String v) {
|
||||
if (String_.Eq(v, Key__raw)) return Io_stream_tid_.Tid__raw;
|
||||
else if (String_.Eq(v, Key__zip)) return Io_stream_tid_.Tid__zip;
|
||||
else if (String_.Eq(v, Key__gzip)) return Io_stream_tid_.Tid__gzip;
|
||||
else if (String_.Eq(v, Key__bzip2)) return Io_stream_tid_.Tid__bzip2;
|
||||
else if (String_.Eq(v, Key__xz)) return Io_stream_tid_.Tid__xz;
|
||||
else throw Err_.new_unhandled_default(v);
|
||||
}
|
||||
public static String Obsolete_to_str(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_tid_.Tid__raw : return ".xdat";
|
||||
case Io_stream_tid_.Tid__zip : return ".zip";
|
||||
case Io_stream_tid_.Tid__gzip : return ".gz";
|
||||
case Io_stream_tid_.Tid__bzip2 : return ".bz2";
|
||||
default : throw Err_.new_unhandled_default(v);
|
||||
}
|
||||
}
|
||||
public static byte Obsolete_to_tid(String v) {
|
||||
if (String_.Eq(v, ".xdat")) return Io_stream_tid_.Tid__raw;
|
||||
else if (String_.Eq(v, ".zip")) return Io_stream_tid_.Tid__zip;
|
||||
else if (String_.Eq(v, ".gz")) return Io_stream_tid_.Tid__gzip;
|
||||
else if (String_.Eq(v, ".bz2")) return Io_stream_tid_.Tid__bzip2;
|
||||
else throw Err_.new_unhandled_default(v);
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
public interface Io_stream_wtr extends Rls_able {
|
||||
byte Tid();
|
||||
Io_url Url(); Io_stream_wtr Url_(Io_url v);
|
||||
void Trg_bfr_(Bry_bfr v);
|
||||
Io_stream_wtr Open();
|
||||
byte[] To_ary_and_clear();
|
||||
byte Tid();
|
||||
Io_url Url(); Io_stream_wtr Url_(Io_url v);
|
||||
void Trg_bfr_(Bry_bfr v);
|
||||
Io_stream_wtr Open();
|
||||
byte[] To_ary_and_clear();
|
||||
|
||||
void Write(byte[] bry, int bgn, int len);
|
||||
void Flush();
|
||||
void Write(byte[] bry, int bgn, int len);
|
||||
void Flush();
|
||||
}
|
||||
|
||||
@@ -16,44 +16,38 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
|
||||
import gplx.core.ios.streams.wtrs.*;
|
||||
public class Io_stream_wtr_ {
|
||||
public static Io_stream_wtr bzip2_(Io_url url) {return new Io_stream_wtr_bzip2().Url_(url);}
|
||||
public static Io_stream_wtr gzip_(Io_url url) {return new Io_stream_wtr_gzip().Url_(url);}
|
||||
public static Io_stream_wtr zip_(Io_url url) {return new Io_stream_wtr_zip().Url_(url);}
|
||||
public static Io_stream_wtr file_(Io_url url) {return new Io_stream_wtr_file().Url_(url);}
|
||||
public static Io_stream_wtr new_by_url_(Io_url url) {
|
||||
public static Io_stream_wtr New__raw(Io_url url) {return new Io_stream_wtr__raw().Url_(url);}
|
||||
private static Io_stream_wtr New__zip(Io_url url) {return new Io_stream_wtr__zip().Url_(url);}
|
||||
private static Io_stream_wtr New__gzip(Io_url url) {return new Io_stream_wtr__gzip().Url_(url);}
|
||||
private static Io_stream_wtr New__bzip2(Io_url url) {return new Io_stream_wtr__bzip2().Url_(url);}
|
||||
public static Io_stream_wtr New_by_url(Io_url url) {
|
||||
String ext = url.Ext();
|
||||
if (String_.Eq(ext, Io_stream_.Ext_zip)) return gplx.core.ios.streams.Io_stream_wtr_.zip_(url);
|
||||
else if (String_.Eq(ext, Io_stream_.Ext_gz)) return gplx.core.ios.streams.Io_stream_wtr_.gzip_(url);
|
||||
else if (String_.Eq(ext, Io_stream_.Ext_bz2)) return gplx.core.ios.streams.Io_stream_wtr_.bzip2_(url);
|
||||
else return gplx.core.ios.streams.Io_stream_wtr_.file_(url);
|
||||
if (String_.Eq(ext, Io_stream_tid_.Ext__zip)) return Io_stream_wtr_.New__zip(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__gz)) return Io_stream_wtr_.New__gzip(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__bz2)) return Io_stream_wtr_.New__bzip2(url);
|
||||
else if (String_.Eq(ext, Io_stream_tid_.Ext__xz)) return new Io_stream_wtr__xz().Url_(url);
|
||||
else return Io_stream_wtr_.New__raw(url);
|
||||
}
|
||||
public static Io_stream_wtr new_by_mem(Bry_bfr bfr, byte tid) {
|
||||
Io_stream_wtr wtr = new_by_tid_(tid).Url_(Io_url_.Empty);
|
||||
public static Io_stream_wtr New_by_tid(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_tid_.Tid__raw: return new Io_stream_wtr__raw();
|
||||
case Io_stream_tid_.Tid__zip: return new Io_stream_wtr__zip();
|
||||
case Io_stream_tid_.Tid__gzip: return new Io_stream_wtr__gzip();
|
||||
case Io_stream_tid_.Tid__bzip2: return new Io_stream_wtr__bzip2();
|
||||
case Io_stream_tid_.Tid__xz: return new Io_stream_wtr__xz();
|
||||
default: throw Err_.new_unhandled(v);
|
||||
}
|
||||
}
|
||||
public static Io_stream_wtr New_by_mem(Bry_bfr bfr, byte tid) {
|
||||
Io_stream_wtr wtr = New_by_tid(tid).Url_(Io_url_.Empty);
|
||||
wtr.Trg_bfr_(bfr);
|
||||
return wtr;
|
||||
}
|
||||
public static Io_stream_wtr new_by_tid_(byte v) {
|
||||
switch (v) {
|
||||
case gplx.core.ios.streams.Io_stream_.Tid_raw : return new Io_stream_wtr_file();
|
||||
case gplx.core.ios.streams.Io_stream_.Tid_zip : return new Io_stream_wtr_zip();
|
||||
case gplx.core.ios.streams.Io_stream_.Tid_gzip : return new Io_stream_wtr_gzip();
|
||||
case gplx.core.ios.streams.Io_stream_.Tid_bzip2 : return new Io_stream_wtr_bzip2();
|
||||
default : throw Err_.new_unhandled(v);
|
||||
}
|
||||
}
|
||||
public static void Save_all(Io_url url, byte[] bry, int bgn, int end) {
|
||||
Io_stream_wtr wtr = new_by_url_(url);
|
||||
try {
|
||||
wtr.Open();
|
||||
wtr.Write(bry, bgn, end);
|
||||
wtr.Flush();
|
||||
}
|
||||
finally {wtr.Rls();}
|
||||
}
|
||||
public static void Save_rdr(Io_url url, Io_stream_rdr rdr, Io_download_fmt download_progress) {
|
||||
byte[] bry = new byte[4096];
|
||||
Io_stream_wtr wtr = new_by_url_(url);
|
||||
Io_stream_wtr wtr = New_by_url(url);
|
||||
try {
|
||||
wtr.Open();
|
||||
if (download_progress != Io_download_fmt.Null)
|
||||
@@ -72,142 +66,3 @@ public class Io_stream_wtr_ {
|
||||
finally {wtr.Rls(); rdr.Rls();}
|
||||
}
|
||||
}
|
||||
abstract class Io_stream_wtr_base implements Io_stream_wtr {
|
||||
java.io.OutputStream zip_stream;
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; trg_bfr = null; return this;} Io_url url;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} Bry_bfr trg_bfr; java.io.ByteArrayOutputStream mem_stream;
|
||||
public byte[] To_ary_and_clear() {return trg_bfr.To_bry_and_clear();}
|
||||
public Io_stream_wtr Open() {
|
||||
java.io.OutputStream bry_stream = null;
|
||||
if (trg_bfr == null) {
|
||||
if (!Io_mgr.Instance.ExistsFil(url)) Io_mgr.Instance.SaveFilStr(url, "");
|
||||
try {bry_stream = new java.io.FileOutputStream(url.Raw());}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
}
|
||||
else {
|
||||
mem_stream = new java.io.ByteArrayOutputStream();
|
||||
bry_stream = mem_stream;
|
||||
}
|
||||
zip_stream = Wrap_stream(bry_stream);
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
try {zip_stream.write(bry, bgn, len);}
|
||||
catch (Exception e) {Err_.new_exc(e, "io", "write failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public void Flush() {
|
||||
if (trg_bfr != null) {
|
||||
try {zip_stream.close();} catch (Exception e) {throw Err_.new_exc(e, "io", "flush failed");} // must close zip_stream to flush all bytes
|
||||
trg_bfr.Add(mem_stream.toByteArray());
|
||||
}
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (zip_stream != null) zip_stream.close();
|
||||
if (mem_stream != null) mem_stream.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
public abstract java.io.OutputStream Wrap_stream(java.io.OutputStream stream);
|
||||
}
|
||||
class Io_stream_wtr_bzip2 extends Io_stream_wtr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_bzip2;}
|
||||
@Override public java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {
|
||||
try {return new org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream(stream);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "failed to open bzip2 stream");}
|
||||
}
|
||||
static final byte[] Bz2_header = new byte[] {Byte_ascii.Ltr_B, Byte_ascii.Ltr_Z};
|
||||
}
|
||||
class Io_stream_wtr_gzip extends Io_stream_wtr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_gzip;}
|
||||
@Override public java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {
|
||||
try {return new java.util.zip.GZIPOutputStream(stream);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "failed to open gz stream");}
|
||||
}
|
||||
}
|
||||
class Io_stream_wtr_zip implements Io_stream_wtr {
|
||||
private java.util.zip.ZipOutputStream zip_stream;
|
||||
@Override public byte Tid() {return Io_stream_.Tid_zip;}
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; trg_bfr = null; return this;} private Io_url url = Io_url_.Empty;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} private Bry_bfr trg_bfr; private java.io.ByteArrayOutputStream mem_stream;
|
||||
// rely on zip_stream to close bry_stream
|
||||
public Io_stream_wtr Open() {
|
||||
java.io.OutputStream bry_stream;
|
||||
if (trg_bfr == null) {
|
||||
if (!Io_mgr.Instance.ExistsFil(url)) Io_mgr.Instance.SaveFilStr(url, ""); // create file if it doesn't exist
|
||||
try {bry_stream = new java.io.FileOutputStream(url.Xto_api());}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
}
|
||||
else {
|
||||
mem_stream = new java.io.ByteArrayOutputStream();
|
||||
bry_stream = mem_stream;
|
||||
}
|
||||
zip_stream = new java.util.zip.ZipOutputStream(bry_stream);
|
||||
java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry("file");
|
||||
try {zip_stream.putNextEntry(entry);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
try {zip_stream.write(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "write failed", "url", url.Raw(), "bgn", bgn, "len", len);}
|
||||
}
|
||||
public void Flush() {// fixed as of DATE:2014-04-15
|
||||
try {
|
||||
zip_stream.closeEntry();
|
||||
zip_stream.close();
|
||||
if (trg_bfr != null)
|
||||
trg_bfr.Add(mem_stream.toByteArray());
|
||||
zip_stream.flush();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "flush failed", "url", url.Raw());}
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (zip_stream != null) zip_stream.close();
|
||||
if (mem_stream != null) mem_stream.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
public byte[] To_ary_and_clear() {
|
||||
byte[] rv = trg_bfr.To_bry_and_clear();
|
||||
this.Rls();
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Io_stream_wtr_file implements Io_stream_wtr {
|
||||
IoStream bry_stream;
|
||||
@Override public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; return this;} Io_url url;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} private Bry_bfr trg_bfr; java.io.ByteArrayOutputStream mem_stream;
|
||||
public Io_stream_wtr Open() {
|
||||
try {
|
||||
if (trg_bfr == null)
|
||||
bry_stream = Io_mgr.Instance.OpenStreamWrite(url);
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
if (trg_bfr == null) {
|
||||
try {bry_stream.Write(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "write failed", "url", url.Raw(), "bgn", bgn, "len", len);}
|
||||
}
|
||||
else
|
||||
trg_bfr.Add_mid(bry, bgn, bgn + len);
|
||||
}
|
||||
public byte[] To_ary_and_clear() {
|
||||
return trg_bfr == null ? Io_mgr.Instance.LoadFilBry(url) : trg_bfr.To_bry_and_clear();
|
||||
}
|
||||
public void Flush() {
|
||||
if (trg_bfr == null)
|
||||
bry_stream.Flush();
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (trg_bfr == null)
|
||||
bry_stream.Rls();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__adp implements Io_stream_rdr {
|
||||
private java.io.InputStream strm;
|
||||
public Io_stream_rdr__adp(java.io.InputStream strm) {this.strm = strm;}
|
||||
public Object Under() {return strm;}
|
||||
public byte Tid() {return Io_stream_tid_.Tid__raw;}
|
||||
public boolean Exists() {return len > 0;}
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {this.url = v; return this;} private Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = Io_mgr.Len_null;
|
||||
public void Open_mem(byte[] v) {}
|
||||
public Io_stream_rdr Open() {return this;}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {return strm.read(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return strm.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {strm.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public abstract class Io_stream_rdr__base implements Io_stream_rdr {
|
||||
public abstract byte Tid();
|
||||
public Io_url Url() {return url;} protected Io_url url;
|
||||
public long Len() {return len;} private long len = Io_mgr.Len_null;
|
||||
public boolean Exists() {return this.Len() > 0;}
|
||||
public Io_stream_rdr Url_(Io_url v) {this.url = v; return this;}
|
||||
public Io_stream_rdr Len_(long v) {len = v; return this;}
|
||||
public Object Under() {return stream;} public Io_stream_rdr Under_(java.io.InputStream v) {this.stream = v; return this;} protected java.io.InputStream stream;
|
||||
public void Open_mem(byte[] v) {
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(v));
|
||||
}
|
||||
public Io_stream_rdr Open() {
|
||||
try {stream = Wrap_stream(new java.io.FileInputStream(url.Xto_api()));}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {return stream.read(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return stream.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {stream.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
}
|
||||
public abstract java.io.InputStream Wrap_stream(java.io.InputStream stream);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__bzip2 extends Io_stream_rdr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__bzip2;}
|
||||
@Override public int Read(byte[] bry, int bgn, int len) {
|
||||
return Io_stream_rdr_.Read_by_parts(stream, Read_len, bry, bgn, len);
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {
|
||||
try {return new org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream(stream, true);}
|
||||
catch (Exception exc) {throw Err_.new_wo_type("failed to open bzip2 stream");}
|
||||
}
|
||||
private static final int Read_len = Io_mgr.Len_mb * 128;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__gzip extends Io_stream_rdr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__gzip;}
|
||||
@Override public int Read(byte[] bry, int bgn, int len) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
int total_read = 0;
|
||||
while (true) { // NOTE: the gz stream reads partially; (request 100; only get back 10); keep reading until entire bfr is full or -1
|
||||
int read = stream.read(bry, bgn, len);
|
||||
if (read == Io_stream_rdr_.Read_done) break;
|
||||
total_read += read;
|
||||
if (total_read >= len) break; // entire bfr full; stop
|
||||
bgn += read; // increase bgn by amount read
|
||||
len -= read; // decrease len by amount read
|
||||
}
|
||||
return total_read == 0 ? Io_stream_rdr_.Read_done : total_read; // gzip seems to allow 0 bytes read (bz2 and zip return -1 instead); normalize return to -1;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {
|
||||
try {return new java.util.zip.GZIPInputStream(stream);}
|
||||
catch (Exception exc) {throw Err_.new_wo_type("failed to open gz stream");}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__noop implements Io_stream_rdr {
|
||||
public Object Under() {return null;}
|
||||
public byte Tid() {return Io_stream_tid_.Tid__null;}
|
||||
public boolean Exists() {return false;}
|
||||
public Io_url Url() {return Io_url_.Empty;} public Io_stream_rdr Url_(Io_url v) {return this;}
|
||||
public long Len() {return Io_mgr.Len_null;} public Io_stream_rdr Len_(long v) {return this;}
|
||||
public void Open_mem(byte[] v) {}
|
||||
public Io_stream_rdr Open() {return this;}
|
||||
public int Read(byte[] bry, int bgn, int len) {return Io_stream_rdr_.Read_done;}
|
||||
public long Skip(long len) {return Io_stream_rdr_.Read_done;}
|
||||
public void Rls() {}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__raw extends Io_stream_rdr__base {
|
||||
public byte Tid() {return Io_stream_tid_.Tid__raw;}
|
||||
public Io_stream_rdr Open() {
|
||||
Io_url url = this.Url();
|
||||
try {
|
||||
if (!Io_mgr.Instance.Exists(url))
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(Bry_.Empty));
|
||||
else {
|
||||
if (url.Info().EngineKey() == IoEngine_.MemKey)
|
||||
stream = Wrap_stream(new java.io.ByteArrayInputStream(Io_mgr.Instance.LoadFilBry(url.Xto_api())));
|
||||
else
|
||||
stream = Wrap_stream(new java.io.FileInputStream(url.Xto_api()));
|
||||
}
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {return stream;}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__xz extends Io_stream_rdr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__xz;}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream stream) {
|
||||
try {return new org.tukaani.xz.XZInputStream(stream);}
|
||||
catch (Exception exc) {throw Err_.new_wo_type("failed to open xz stream");}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.rdrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr__zip extends Io_stream_rdr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__gzip;}
|
||||
public Object Under() {return zip_stream;} private java.util.zip.ZipInputStream zip_stream;
|
||||
public void Src_bfr_(Bry_bfr v) {this.src_bfr = v;} Bry_bfr src_bfr;
|
||||
public void Open_mem(byte[] v) {
|
||||
this.zip_stream = (java.util.zip.ZipInputStream)Wrap_stream(new java.io.ByteArrayInputStream(v));
|
||||
}
|
||||
public Io_stream_rdr Open() {
|
||||
try {this.zip_stream = (java.util.zip.ZipInputStream)Wrap_stream(new java.io.FileInputStream(url.Xto_api()));}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Xto_api());}
|
||||
return this;
|
||||
}
|
||||
public int Read(byte[] bry, int bgn, int len) {
|
||||
try {
|
||||
while (true){
|
||||
int read = zip_stream.read(bry, bgn, len);
|
||||
if (read == Io_stream_rdr_.Read_done) {
|
||||
if (zip_stream.getNextEntry() == null)
|
||||
return Io_stream_rdr_.Read_done;
|
||||
}
|
||||
else
|
||||
return read;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "read failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public long Skip(long len) {
|
||||
try {return zip_stream.skip(len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "skip failed", "len", len);}
|
||||
}
|
||||
public void Rls() {
|
||||
try {zip_stream.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Xto_api());}
|
||||
}
|
||||
@Override public java.io.InputStream Wrap_stream(java.io.InputStream input_stream) {return new java.util.zip.ZipInputStream(input_stream);}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public abstract class Io_stream_wtr__base implements Io_stream_wtr {
|
||||
public abstract byte Tid();
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
public Io_stream_wtr Url_(Io_url v) {url = v; return this;}
|
||||
public void Trg_bfr_(Bry_bfr v) {this.trg_bfr = v;} private Bry_bfr trg_bfr;
|
||||
public byte[] To_ary_and_clear() {return trg_bfr.To_bry_and_clear();}
|
||||
|
||||
private java.io.OutputStream zip_stream;
|
||||
private java.io.ByteArrayOutputStream mem_stream;
|
||||
@Virtual public Io_stream_wtr Open() {
|
||||
java.io.OutputStream bry_stream = null;
|
||||
if (trg_bfr == null) {
|
||||
if (!Io_mgr.Instance.ExistsFil(url)) Io_mgr.Instance.SaveFilStr(url, "");
|
||||
try {bry_stream = new java.io.FileOutputStream(url.Raw());}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
}
|
||||
else {
|
||||
mem_stream = new java.io.ByteArrayOutputStream();
|
||||
bry_stream = mem_stream;
|
||||
}
|
||||
zip_stream = Wrap_stream(bry_stream);
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
try {zip_stream.write(bry, bgn, len);}
|
||||
catch (Exception e) {Err_.new_exc(e, "io", "write failed", "bgn", bgn, "len", len);}
|
||||
}
|
||||
public void Flush() {
|
||||
if (trg_bfr != null) {
|
||||
try {zip_stream.close();} catch (Exception e) {throw Err_.new_exc(e, "io", "flush failed");} // must close zip_stream to flush all bytes
|
||||
trg_bfr.Add(mem_stream.toByteArray());
|
||||
}
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (zip_stream != null) zip_stream.close();
|
||||
if (mem_stream != null) mem_stream.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
@Virtual protected java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {throw Err_.new_unimplemented();}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_wtr__bzip2 extends Io_stream_wtr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__bzip2;}
|
||||
@Override public java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {
|
||||
try {return new org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream(stream);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "failed to open bzip2 stream");}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_wtr__gzip extends Io_stream_wtr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__gzip;}
|
||||
@Override public java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {
|
||||
try {return new java.util.zip.GZIPOutputStream(stream);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "failed to open gz stream");}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_wtr__raw implements Io_stream_wtr {
|
||||
public byte Tid() {return Io_stream_tid_.Tid__raw;}
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; return this;} private Io_url url;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} private Bry_bfr trg_bfr;
|
||||
|
||||
private IoStream bry_stream;
|
||||
@Override public Io_stream_wtr Open() {
|
||||
try {
|
||||
if (trg_bfr == null)
|
||||
bry_stream = Io_mgr.Instance.OpenStreamWrite(url);
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
if (trg_bfr == null) {
|
||||
try {bry_stream.Write(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "write failed", "url", url.Raw(), "bgn", bgn, "len", len);}
|
||||
}
|
||||
else
|
||||
trg_bfr.Add_mid(bry, bgn, bgn + len);
|
||||
}
|
||||
public byte[] To_ary_and_clear() {
|
||||
return trg_bfr == null ? Io_mgr.Instance.LoadFilBry(url) : trg_bfr.To_bry_and_clear();
|
||||
}
|
||||
public void Flush() {
|
||||
if (trg_bfr == null)
|
||||
bry_stream.Flush();
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (trg_bfr == null)
|
||||
bry_stream.Rls();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_wtr__xz extends Io_stream_wtr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__xz;}
|
||||
@Override public java.io.OutputStream Wrap_stream(java.io.OutputStream stream) {
|
||||
try {return new org.tukaani.xz.XZOutputStream(stream, new org.tukaani.xz.LZMA2Options(org.tukaani.xz.LZMA2Options.PRESET_DEFAULT));}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "failed to open xz stream");}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios.streams.wtrs; import gplx.*; import gplx.core.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_stream_wtr__zip extends Io_stream_wtr__base {
|
||||
@Override public byte Tid() {return Io_stream_tid_.Tid__zip;}
|
||||
private java.util.zip.ZipOutputStream zip_stream;
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; trg_bfr = null; return this;} private Io_url url = Io_url_.Empty;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} private Bry_bfr trg_bfr; private java.io.ByteArrayOutputStream mem_stream;
|
||||
// rely on zip_stream to close bry_stream
|
||||
@Override public Io_stream_wtr Open() {
|
||||
java.io.OutputStream bry_stream;
|
||||
if (trg_bfr == null) {
|
||||
if (!Io_mgr.Instance.ExistsFil(url)) Io_mgr.Instance.SaveFilStr(url, ""); // create file if it doesn't exist
|
||||
try {bry_stream = new java.io.FileOutputStream(url.Xto_api());}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
}
|
||||
else {
|
||||
mem_stream = new java.io.ByteArrayOutputStream();
|
||||
bry_stream = mem_stream;
|
||||
}
|
||||
zip_stream = new java.util.zip.ZipOutputStream(bry_stream);
|
||||
java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry("file");
|
||||
try {zip_stream.putNextEntry(entry);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "open failed", "url", url.Raw());}
|
||||
return this;
|
||||
}
|
||||
public void Write(byte[] bry, int bgn, int len) {
|
||||
try {zip_stream.write(bry, bgn, len);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "write failed", "url", url.Raw(), "bgn", bgn, "len", len);}
|
||||
}
|
||||
public void Flush() {// fixed as of DATE:2014-04-15
|
||||
try {
|
||||
zip_stream.closeEntry();
|
||||
zip_stream.close();
|
||||
if (trg_bfr != null)
|
||||
trg_bfr.Add(mem_stream.toByteArray());
|
||||
zip_stream.flush();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "flush failed", "url", url.Raw());}
|
||||
}
|
||||
public void Rls() {
|
||||
try {
|
||||
if (zip_stream != null) zip_stream.close();
|
||||
if (mem_stream != null) mem_stream.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "io", "close failed", "url", url.Raw());}
|
||||
}
|
||||
public byte[] To_ary_and_clear() {
|
||||
byte[] rv = trg_bfr.To_bry_and_clear();
|
||||
this.Rls();
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user