1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2014-07-13 23:23:30 -04:00
parent ecbe2918d8
commit bc10cd76b6
316 changed files with 3251 additions and 1652 deletions

View File

@@ -0,0 +1,71 @@
/*
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.ios; import gplx.*;
import gplx.ios.*;/*IoStream*/
public class Io_buffer_rdr implements RlsAble { //_20120115
Io_buffer_rdr(Io_stream_rdr rdr, Io_url url, int bfr_len) {
if (bfr_len <= 0) throw Err_.new_("bfr_len must be > 0").Add("bfr_len", bfr_len);
bfr = new byte[bfr_len]; this.bfr_len = bfr_len;
this.rdr = rdr;
IoItmFil fil = Io_mgr._.QueryFil(url); if (!fil.Exists()) throw Err_.new_("fil does not exist").Add("url", url);
fil_len = fil.Size();
fil_pos = 0;
fil_eof = false;
} Io_stream_rdr rdr;
public byte[] Bfr() {return bfr;} private byte[] bfr;
public int Bfr_len() {return bfr_len;} private int bfr_len;
public long Fil_len() {return fil_len;} long fil_len;
public long Fil_pos() {return fil_pos;} long fil_pos;
public boolean Fil_eof() {return fil_eof;} private boolean fil_eof;
public boolean Bfr_load_all() {return Bfr_load(0, bfr_len);}
public boolean Bfr_load_from(int bfr_pos) {
if (bfr_pos < 0 || bfr_pos > bfr_len) throw Err_.new_("invalid bfr_pos").Add("bfr_pos", bfr_pos).Add("bfr_len", bfr_len);
for (int i = bfr_pos; i < bfr_len; i++) // shift end of bfr to bgn; EX: bfr[10] and load_from(8); [8] -> [0]; [9] -> [1];
bfr[i - bfr_pos] = bfr[i];
return Bfr_load(bfr_len - bfr_pos, bfr_pos); // fill rest of bfr; EX: [2]... will come from file
}
boolean Bfr_load(int bgn, int len) {
int read = rdr.Read(bfr, bgn, len);
if (read == gplx.ios.Io_stream_rdr_.Read_done) {fil_eof = true; return false;}
fil_pos += read;
bfr_len = bgn + read;
if (read < len) fil_eof = true;
return true;
}
public void Seek(long fil_pos) {
this.fil_pos = fil_pos;
rdr.Skip(fil_pos);
this.Bfr_load_all();
}
public void Rls() {
bfr = null;
bfr_len = -1;
if (rdr != null) rdr.Rls();
}
@gplx.Internal protected void DumpToFil(int bgn, int len, String urlStr, String msg) {
String text = String_.new_utf8_len_safe_(bfr, bgn, len);
Io_mgr._.AppendFilStr(Io_url_.new_any_(urlStr), msg + text + "\n");
}
public static Io_buffer_rdr new_(Io_stream_rdr rdr, int bfr_len) {
Io_buffer_rdr rv = new Io_buffer_rdr(rdr, rdr.Url(), bfr_len);
rdr.Open();
rv.Bfr_load(0, bfr_len);
return rv;
} Io_buffer_rdr() {}
public static final Io_buffer_rdr Null = new Io_buffer_rdr();
}

View File

@@ -0,0 +1,64 @@
/*
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.ios; import gplx.*;
import org.junit.*; import gplx.ios.*;
public class Io_buffer_rdr_tst {
@Before public void init() {
Io_mgr._.InitEngine_mem();
fil = Io_url_.mem_fil_("mem/byteStreamRdr.txt");
ini_Write("0123456789");
rdr = Io_buffer_rdr.new_(Io_stream_rdr_.file_(fil), 4);
} Io_buffer_rdr rdr; Io_url fil;
@After public void teardown() {rdr.Rls();}
@Test public void Bfr_load_all() {
tst_Bfr("0", "1", "2", "3").tst_ReadDone(false);
rdr.Bfr_load_all();
tst_Bfr("4", "5", "6", "7").tst_ReadDone(false);
rdr.Bfr_load_all();
tst_Bfr("8", "9");
rdr.Bfr_load_all(); // NOTE: change to zip_rdrs make eof detection difficult; force another load to ensure that file_pos goes past file_len
tst_ReadDone(true); // NOTE: bfr truncated from 4 to 2
}
@Test public void Bfr_load_from() {
tst_Bfr("0", "1", "2", "3").tst_ReadDone(false);
rdr.Bfr_load_from(3); // read from pos 3
tst_Bfr("3", "4", "5", "6").tst_ReadDone(false);
rdr.Bfr_load_from(1); // read from pos 1
tst_Bfr("4", "5", "6", "7").tst_ReadDone(false);
rdr.Bfr_load_from(1);
tst_Bfr("5", "6", "7", "8").tst_ReadDone(false);
rdr.Bfr_load_from(3);
rdr.Bfr_load_all(); // NOTE: change to zip_rdrs make eof detection difficult; force another load to ensure that file_pos goes past file_len
tst_Bfr("8", "9").tst_ReadDone(true);
}
private void ini_Write(String s) {Io_mgr._.SaveFilStr(fil, s);}
Io_buffer_rdr_tst tst_Bfr(String... expdAry) {
String[] actlAry = new String[rdr.Bfr_len()];
for (int i = 0; i < actlAry.length; i++)
actlAry[i] = String_.new_utf8_(rdr.Bfr(), i, i + 1);
Tfds.Eq_ary(expdAry, actlAry);
return this;
}
Io_buffer_rdr_tst tst_ReadDone(boolean expd) {Tfds.Eq(expd, rdr.Fil_eof()); return this;}
}