日記を埋めるためだけのネタつぶし(7)

昨日の日記に書いた通信クラスの使用方法。
Doja3.0の最大ダウンロードが20KBのため、ほぼ固定でのデータダウンロードとなっている。
汎用性があまりないので、通信クラスのデータダウンロードはバッファのサイズとオフセットを指定できるようにするなど
すると、もっと汎用性が取れると思われる。


// 文字列取得
final public void run() {
CHttp hc = null;
int state = 0;
String rstr;

while (true) {
switch (state) {
// 接続開始
case 0 :
state = 1;
hc = new CHttp();
hc.connect("http://・・・/*.php"); // 接続先を渡す
break;
// 接続中
case 1 :
if (!hc.nowConnect()) {
// 接続終了
state = 2;
if (hc.getError() != null) {
// エラーあり
rstr = null;
} else {
// エラーなし
rstr = hc.getString(); // phpから返ってきた文字列を取得
}
} else {
// 接続中の処理を記述
}
break;
// 接続終了
case 2 :
break;
}
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
// バイナリ取得
final public void run() {
CHttp hc = null;
int state = 0;
int length;
byte buf[] = new byte[20 * 1024];

while (true) {
switch (state) {
// 接続開始
case 0 :
state = 1;
hc = new CHttp();
hc.connect("http//・・・/*.dat", buf); // 接続先を渡す
break;
// 接続中
case 1 :
if (!hc.nowConnect()) {
// 接続終了
state = 2;
if (hc.getError() != null) {
// エラーあり
rstr = null;
} else {
// エラーなし
length = hc.getLength(); // ダウンロードしたバイト数を取得。bufには、すでにデータが入っている。
}
} else {
// 接続中の処理を記述
}
break;
// 接続終了
case 2 :
break;
}
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}