Ifvwm
08/05/2019, 3:40 AMgildor
08/05/2019, 3:42 AMclean
? What do you want to achieve?Ifvwm
08/05/2019, 3:44 AMIfvwm
08/05/2019, 3:45 AMIfvwm
08/05/2019, 3:46 AMIfvwm
08/05/2019, 3:47 AMgildor
08/05/2019, 5:40 AMand when it received new messages, it will read the old messagesIt’s not clear why it would work like that, it highly depends on your code, but I see problems with this behavior in general you already get this data from client and handle it somehow. If it was successful chunk of data you just pass it to processing, if it was error, you throw error, why would code read data from buffer again? Instead you should write it to buffer from socket, and read from buffer Anyway, the only way is to set values to 0 for this array is set every element to 0 or create a new one
gildor
08/05/2019, 5:40 AMfun ByteArray.setAll(value: Byte) {
for (i in 0 until size) {
this[i] = value
}
}
karelpeeters
08/05/2019, 6:32 AMgildor
08/05/2019, 6:50 AMthey have a fixed easily obtained size instead unlike CI’m not so familiar with C, but isn’t you also have fixed array size in C?
karelpeeters
08/05/2019, 6:54 AMgildor
08/05/2019, 6:59 AMmsink
08/05/2019, 7:27 AMcbruegg
08/05/2019, 8:58 AMIfvwm
08/05/2019, 12:25 PMvar aob = ByteArray(1024)
while (true) {
try{
size = client.inputStream.read(aob)
}catch(e:Exception){
client.close()
client = re_connect_network()
}
recvMsg = String(aob.sliceArray(0 until size), Charsets.UTF_8)
if recvMsg.contains(“something”) doSomthing
...
}
that’s how I handle it, but when the connection is disconnected without an exit signal, so this try-catch will work and make a new connection, but the last received recvMsg will still go through those next if-else, so that’s I wonder if there’s a way can clear aob the ByteArray?
also I find a way to solve this, just set size to 0, so sliceArray will not work, this can avoid those next if-elsecbruegg
08/05/2019, 12:27 PMcatch
block: aob = ByteArray(1024)
. It's likely that the overhead of the exception is far higher than the cost of re-allocating an array, and also zeroing an existing array in code might actually be slower.gildor
08/05/2019, 1:30 PMtry
?