admin管理员组文章数量:1402544
I could not find on Android official documentation any reference about, what is the maximum String lenght allowed to be placed in the text content ? ( not in the Tag, of course ).
In the case of not being possible to deal with Strings larger than somewhat ~250K bytes, is there any option to do that ?
BTW, I'm trying to debug a huge message pulled from the socket. Although the original image is not that big, after being 'base64 encoded', its raw UTF-8 sentence has considerably increased the byte amount, compared to the file size.
I could not find on Android official documentation any reference about, what is the maximum String lenght allowed to be placed in the text content ? ( not in the Tag, of course ).
In the case of not being possible to deal with Strings larger than somewhat ~250K bytes, is there any option to do that ?
BTW, I'm trying to debug a huge message pulled from the socket. Although the original image is not that big, after being 'base64 encoded', its raw UTF-8 sentence has considerably increased the byte amount, compared to the file size.
Share Improve this question edited Mar 21 at 23:38 tyg 16.6k4 gold badges37 silver badges49 bronze badges asked Mar 21 at 23:23 aluis.rcastroaluis.rcastro 961 silver badge8 bronze badges3 Answers
Reset to default 3I don't know the exact amount (and it differs by OS version and likely OEM), but log isn't going to work on anything remotely near 250K. Nor even if it could would that be a good decision- the logger is a system wide shared resource. Placing that much in it would make it unusable for anything else in the system. If you need to log that much data, you need to create your own logging module that logs to a file.
And yes, base64 encoding always increases the size of data by about 1/3. Which is why you should almost never use it. Base64 was a hack to be able to put binary data in http requests and other network protocols which were expected to be text only. Basically working around the limitation of it being text only, which was already a hack of using protocols like http instead of a real network protocol for transferring binary data in order to get around firewall restrictions. If you don't absolutely have to base64 encode things, don't do it. If you do have to, you've already decided you're ok with the greater size.
Just to share, I was able to achieve the intended result by making a little change in the TcpClient() method, by chunking the received message on the fly as were receiving it. Note that itwas possible to do even with a tinny byte array of only 1024 bytes to hold the incoming data:
package com.valid.insightsiscopam.api
import android.util.Log
import com.MYCOMPANY.MYPRJECT.constants.Constants
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStreamReader
import java.io.PrintWriter
import java.Socket
import java.nio.charset.Charset
class TcpClient(private val serverIp: String, private val serverPort: Int) {
// Define timeouts in milliseconds
private val connectionTimeout = 5000 // 5 seconds
private val readTimeout = 5000 // 5 seconds
suspend fun sendMessage(message: String): String {
return withContext(Dispatchers.IO) {
try {
val socket = Socket()
socket.connect(java.InetSocketAddress(serverIp, serverPort), connectionTimeout)
// Set the read timeout
socket.soTimeout = readTimeout
// Create output stream to send data to the server
val out = PrintWriter(socket.getOutputStream(), true)
out.println(message) // Send the message
// Create input stream to receive data from the server
val input = socket.getInputStream()
val buffer = ByteArray(1024)
val stringBuilder = StringBuilder()
var bytesRead: Int
// Read data in chunks
while (true)
{
bytesRead = input.read(buffer) // Read bytes into buffer
if (bytesRead == -1) break // End of stream
val chunk = String(buffer, 0, bytesRead, Charset.forName("UTF-8"))
stringBuilder.append(chunk)
Log.d(Constants.LOG_MYPROJECT, "$chunk") // Print each chunk or process it as needed
}
// Close the streams and socket
out.close()
input.close()
socket.close()
// Return the complete response
stringBuilder.toString()
} catch (e: Exception) {
Log.e(Constants.LOG_MYPROJECT, "Error: ${e.message}")
"Error: ${e.message}" // Return the error message
}
}
}
}
android.util.Log is mainly used to print to console/logcat. maybe you should consider the system shared buffer size limit. For any program in general, the contents of the log should not be too large, I don't think the maximum should be more than 1024 bytes, and the recommended value is no more than 256 bytes. If you need to enter it into the log, you should consider persisting it to your disk.
本文标签: kotlinWhat39s the maximum string length one can log using androidutilLogStack Overflow
版权声明:本文标题:kotlin - What's the maximum string length one can log using android.util.Log? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744329733a2600903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论