mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
'v3.7.1.1'
This commit is contained in:
@@ -38,7 +38,11 @@ public class Http_download_wkr__jre extends Http_download_wkr__base {
|
||||
InputStream src_stream = null;
|
||||
URL src_url_itm = null;
|
||||
try {src_url_itm = new URL(src_url);}
|
||||
catch (MalformedURLException e) {throw Err_.new_("download_file", "bad url", "src", src_url, "err" + e.toString());}
|
||||
catch (MalformedURLException e) {
|
||||
try {trg_stream.close();}
|
||||
catch (IOException e1) {}
|
||||
throw Err_.new_("download_file", "bad url", "src", src_url, "err" + e.toString());
|
||||
}
|
||||
HttpURLConnection src_conn = null;
|
||||
try {
|
||||
// open connection
|
||||
@@ -50,12 +54,18 @@ public class Http_download_wkr__jre extends Http_download_wkr__base {
|
||||
// check response code
|
||||
int response_code = src_conn.getResponseCode();
|
||||
if (prog_resumed) {
|
||||
if (response_code != HttpURLConnection.HTTP_PARTIAL)
|
||||
if (response_code != HttpURLConnection.HTTP_PARTIAL) {
|
||||
try {trg_stream.close();}
|
||||
catch (IOException e1) {}
|
||||
throw Err_.new_("download_file", "server returned non-partial response code", "src", src_url, "code", src_conn.getResponseCode(), "msg", src_conn.getResponseMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (response_code != HttpURLConnection.HTTP_OK)
|
||||
if (response_code != HttpURLConnection.HTTP_OK) {
|
||||
try {trg_stream.close();}
|
||||
catch (IOException e1) {}
|
||||
throw Err_.new_("download_file", "server returned non-OK response code", "src", src_url, "code", src_conn.getResponseCode(), "msg", src_conn.getResponseMessage());
|
||||
}
|
||||
}
|
||||
src_stream = src_conn.getInputStream();
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,22 +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.threads.poolables; import gplx.*; import gplx.core.*; import gplx.core.threads.*;
|
||||
public interface Gfo_poolable_itm {
|
||||
Gfo_poolable_itm Pool__make (Gfo_poolable_mgr mgr, int idx, Object[] args);
|
||||
void Pool__rls();
|
||||
}
|
||||
@@ -1,90 +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.threads.poolables; import gplx.*; import gplx.core.*; import gplx.core.threads.*;
|
||||
public class Gfo_poolable_mgr {
|
||||
private final Object thread_lock = new Object();
|
||||
private final Gfo_poolable_itm prototype; private final Object[] make_args;
|
||||
private Gfo_poolable_itm[] pool; private int pool_nxt, pool_len;
|
||||
public Gfo_poolable_mgr(Gfo_poolable_itm prototype, Object[] make_args, int init_pool_len, int pool_max) {// NOTE: random IndexOutOfBounds errors in Get around free_ary[--free_len] with free_len being -1; put member variable initialization within thread_lock to try to avoid; DATE:2014-09-21
|
||||
this.prototype = prototype; this.make_args = make_args;
|
||||
this.pool_len = init_pool_len;
|
||||
this.Clear_fast();
|
||||
}
|
||||
public void Clear_safe() {synchronized (thread_lock) {Clear_fast();}}
|
||||
public void Clear_fast() {
|
||||
this.pool = new Gfo_poolable_itm[pool_len];
|
||||
for (int i = 0; i < pool_len; ++i)
|
||||
pool[i] = prototype.Pool__make(this, i, make_args);
|
||||
this.free_ary = new int[pool_len];
|
||||
pool_nxt = free_len = 0;
|
||||
}
|
||||
public Gfo_poolable_itm Get_safe() {synchronized (thread_lock) {return Get_fast();}}
|
||||
public Gfo_poolable_itm Get_fast() {
|
||||
Gfo_poolable_itm rv = null;
|
||||
int pool_idx = -1;
|
||||
if (free_len > 0) { // free_itms in pool; use it
|
||||
pool_idx = free_ary[--free_len];
|
||||
rv = pool[pool_idx];
|
||||
}
|
||||
else { // nothing in pool; take next
|
||||
if (pool_nxt == pool_len)
|
||||
Expand_pool();
|
||||
pool_idx = pool_nxt++;
|
||||
rv = pool[pool_idx];
|
||||
if (rv == null) {
|
||||
rv = prototype.Pool__make(this, pool_idx, make_args);
|
||||
pool[pool_idx] = rv;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Rls_safe(int idx) {synchronized (thread_lock) {Rls_safe(idx);}}
|
||||
public void Rls_fast(int idx) {
|
||||
if (idx == -1) throw Err_.new_wo_type("rls called on poolable that was not created by pool_mgr");
|
||||
int pool_idx = pool_nxt - 1;
|
||||
if (idx == pool_idx) // in-sequence; decrement count
|
||||
if (free_len == 0)
|
||||
this.pool_nxt = pool_idx;
|
||||
else { // idx == pool_idx; assume entire pool released, but out of order
|
||||
for (int i = 0; i < free_len; ++i)
|
||||
free_ary[i] = 0;
|
||||
free_len = 0;
|
||||
}
|
||||
else { // out-of-sequence
|
||||
free_ary[free_len] = idx;
|
||||
++free_len;
|
||||
}
|
||||
}
|
||||
private void Expand_pool() {
|
||||
// expand pool
|
||||
int new_pool_len = pool_len == 0 ? 2 : pool_len * 2;
|
||||
Gfo_poolable_itm[] new_pool = new Gfo_poolable_itm[new_pool_len];
|
||||
Array_.Copy_to(pool, 0, new_pool, 0, pool_len);
|
||||
this.pool = new_pool;
|
||||
this.pool_len = new_pool_len;
|
||||
|
||||
// expand free_ary to same len
|
||||
int[] new_free = new int[pool_len];
|
||||
Array_.Copy_to(free_ary, 0, new_free, 0, free_len);
|
||||
this.free_ary = new_free;
|
||||
}
|
||||
@gplx.Internal protected int[] Free_ary() {return free_ary;} private int[] free_ary; private int free_len;
|
||||
@gplx.Internal protected int Free_len() {return free_len;}
|
||||
@gplx.Internal protected int Pool_len() {return pool_len;}
|
||||
@gplx.Internal protected int Pool_nxt() {return pool_nxt;}
|
||||
}
|
||||
@@ -1,22 +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.threads.poolables; import gplx.*; import gplx.core.*; import gplx.core.threads.*;
|
||||
public class Gfo_poolable_mgr_ {
|
||||
public static Gfo_poolable_mgr New(int len, int max, Gfo_poolable_itm prototype) {return new Gfo_poolable_mgr(prototype, Object_.Ary_empty, len, max);}
|
||||
public static Gfo_poolable_mgr New(int len, int max, Gfo_poolable_itm prototype, Object[] make_args) {return new Gfo_poolable_mgr(prototype, make_args, len, max);}
|
||||
}
|
||||
@@ -1,83 +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.threads.poolables; import gplx.*; import gplx.core.*; import gplx.core.threads.*;
|
||||
import org.junit.*;
|
||||
public class Gfo_poolable_mgr_tst {
|
||||
private final Gfo_poolable_mgr_tstr tstr = new Gfo_poolable_mgr_tstr();
|
||||
@Before public void init() {tstr.Clear();}
|
||||
@Test public void Get__one() {
|
||||
tstr.Test__get(0);
|
||||
tstr.Test__free__len(0);
|
||||
tstr.Test__pool__len(2);
|
||||
}
|
||||
@Test public void Get__many__expand() {
|
||||
tstr.Test__get(0);
|
||||
tstr.Test__get(1);
|
||||
tstr.Test__get(2);
|
||||
tstr.Test__free__len(0);
|
||||
tstr.Test__pool__len(4);
|
||||
}
|
||||
@Test public void Rls__lifo() {
|
||||
tstr.Test__get(0);
|
||||
tstr.Test__get(1);
|
||||
tstr.Test__get(2);
|
||||
tstr.Exec__rls(2);
|
||||
tstr.Exec__rls(1);
|
||||
tstr.Exec__rls(0);
|
||||
tstr.Test__pool__nxt(0);
|
||||
tstr.Test__free__len(0);
|
||||
}
|
||||
@Test public void Rls__fifo() {
|
||||
tstr.Test__get(0);
|
||||
tstr.Test__get(1);
|
||||
tstr.Test__get(2);
|
||||
tstr.Exec__rls(0);
|
||||
tstr.Exec__rls(1);
|
||||
tstr.Test__pool__nxt(3);
|
||||
tstr.Test__free__len(2); // 2 items in free_ary
|
||||
tstr.Test__free__ary(0, 1, 0, 0);
|
||||
|
||||
tstr.Test__get(1);
|
||||
tstr.Exec__rls(1);
|
||||
|
||||
tstr.Exec__rls(2);
|
||||
tstr.Test__free__len(0); // 0 items in free_ary
|
||||
tstr.Test__free__ary(0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
class Gfo_poolable_mgr_tstr {
|
||||
private final Gfo_poolable_mgr mgr = new Gfo_poolable_mgr(new Sample_poolable_itm(null, -1, Object_.Ary_empty), Object_.Ary("make"), 2, 8);
|
||||
public void Clear() {mgr.Clear_fast();}
|
||||
public void Test__get(int expd_idx) {
|
||||
Sample_poolable_itm actl_itm = (Sample_poolable_itm)mgr.Get_fast();
|
||||
Tfds.Eq(expd_idx, actl_itm.Pool__idx(), "pool_idx");
|
||||
}
|
||||
public void Test__free__ary(int... expd) {Tfds.Eq_ary(expd, mgr.Free_ary(), "mgr.Free_ary()");}
|
||||
public void Test__free__len(int expd) {Tfds.Eq(expd, mgr.Free_len(), "mgr.Free_len()");}
|
||||
public void Test__pool__len(int expd) {Tfds.Eq(expd, mgr.Pool_len(), "mgr.Pool_len()");}
|
||||
public void Test__pool__nxt(int expd) {Tfds.Eq(expd, mgr.Pool_nxt(), "mgr.Pool_nxt()");}
|
||||
public void Exec__rls(int idx) {mgr.Rls_fast(idx);}
|
||||
}
|
||||
class Sample_poolable_itm implements Gfo_poolable_itm {
|
||||
private Gfo_poolable_mgr pool_mgr;
|
||||
public Sample_poolable_itm(Gfo_poolable_mgr pool_mgr, int pool_idx, Object[] make_args) {this.pool_mgr = pool_mgr; this.pool_idx = pool_idx; this.pool__make_args = make_args;}
|
||||
public int Pool__idx() {return pool_idx;} private final int pool_idx;
|
||||
public Object[] Pool__make_args() {return pool__make_args;} private final Object[] pool__make_args;
|
||||
public void Pool__rls() {pool_mgr.Rls_safe(pool_idx);}
|
||||
public Gfo_poolable_itm Pool__make (Gfo_poolable_mgr mgr, int idx, Object[] args) {return new Sample_poolable_itm(pool_mgr, idx, args);}
|
||||
}
|
||||
Reference in New Issue
Block a user