All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avito.http.internal.Buffers.kt Maven / Gradle / Ivy

Go to download

Collection of infrastructure libraries and gradle plugins of Avito Android project

There is a newer version: 2023.22
Show newest version
package com.avito.http.internal

import okio.Buffer
import java.io.EOFException

/**
 * Returns true if the body probably contains human readable text. Uses a small sample
 * of code points to detect unicode control characters commonly used in binary file signatures.
 */
@Suppress("MagicNumber")
public fun Buffer.isPlaintext(): Boolean {
    try {
        val prefix = Buffer()
        val byteCount = if (buffer.size < 64) buffer.size else 64
        buffer.copyTo(prefix, 0, byteCount)
        @Suppress("UnusedPrivateMember")
        for (i in 0..15) {
            if (prefix.exhausted()) {
                break
            }
            val codePoint = prefix.readUtf8CodePoint()
            if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
                return false
            }
        }
        return true
    } catch (e: EOFException) {
        return false // Truncated UTF-8 sequence.
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy