1
0
mirror of https://github.com/fumiama/simple-dict-android.git synced 2026-06-08 03:55:10 +08:00
This commit is contained in:
fumiama
2021-02-18 00:27:11 +08:00
parent 85428c591c
commit c82f715f55
8 changed files with 41 additions and 30 deletions

View File

@@ -57,27 +57,27 @@ class Client(val ip: String, val port: Int) {
}
}
fun receiveRawMessage()= try {
fun receiveRawMessage() : ByteArray {
var re = byteArrayOf()
try {
if (isConnect) {
Log.d("MyC", "开始接收服务端信息")
val inMessage = ByteArray(1024) //设置接受缓冲,避免接受数据过长占用过多内存
val a = din?.read(inMessage) //a存储返回消息的长度
if (a == null || a <= -1) null
else {
Log.d("MyC", "reply length:$a")
inMessage.copyOf(a)
}
} else {
Log.d("MyC", "no connect to receive message")
null
}
val inMessage = ByteArray(4096) //设置接受缓冲,避免接受数据过长占用过多内存
var a: Int
do {
a = din?.read(inMessage)?:0 //a存储返回消息的长度
Log.d("MyC", "reply length:$a: ${inMessage.decodeToString()}")
re += inMessage.copyOf(a)
} while (a == 4096)
} else Log.d("MyC", "no connect to receive message")
} catch (e: IOException) {
Log.d("MyC", "receive message failed")
e.printStackTrace()
null
}
return re
}
fun receiveMessage() = receiveRawMessage()?.decodeToString()
fun receiveMessage() = receiveRawMessage().decodeToString()
/**
* 关闭连接

View File

@@ -6,13 +6,13 @@ import java.lang.Thread.sleep
class SimpleDict(private val client: Client, private val pwd: String) { //must run in thread
private var dict = HashMap<String, String?>()
val keys get() = dict.keys
val values get() = dict.values
//val values get() = dict.values
//val size get() = dict.size
private val raw: ByteArray?
private val raw: ByteArray
get() {
initDict()
client.sendMessage("cat")
sleep(233)
sleep(2333)
val re = client.receiveRawMessage()
closeDict()
return re
@@ -47,7 +47,7 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
fun fetchDict() {
val dictBlock = ByteArray(128)
raw?.inputStream()?.let {
raw.inputStream().let {
var c = '1'
while (!it.read().toChar().isDigit()) Log.d("MySD", "Skip banner.")
while (c.isDigit()) {

View File

@@ -18,6 +18,7 @@
android:layout_height="match_parent"
app:contentScrim="?attr/colorSurface"
app:layout_scrollFlags="scroll"
app:toolbarId="@+id/toolbar">
<com.lapism.search.widget.MaterialSearchView

View File

@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"

View File

@@ -10,7 +10,7 @@
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>