1

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.

3 Answers 3

3

I 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, I would never consider to put such an amount of data there in the Log() argument, and the reasonable deed is to split the data on the fly as are being received, but I don't even know whether I should split it in 1/10 (25K) or in 1/100 (2.5K) and so on. In shorts, it is weird not to have mention about that anywhere.
Either of those numbers are far too big. The log system is meant for short human readable log statements. And it truncates if its too long. Consider abou8t 200 chars the limit. In other words, this is not what you should be using at all.
0

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.net.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.net.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
            }
        }
    }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.