forked from ChuckerTeam/chucker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract plain text verification to Okio utils * Move Okio utils tests to a separate suite * Add non ASCII tests to plain text verification * Use built-in charset constant * Inline response variable * Extract supported encoding verification to OkHttp utils * Extract uncompressed source to OkHttp utils
- Loading branch information
Showing
7 changed files
with
180 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
library/src/main/java/com/chuckerteam/chucker/internal/support/OkioUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import okio.Buffer | ||
import java.io.EOFException | ||
import kotlin.math.min | ||
|
||
private const val MAX_PREFIX_SIZE = 64L | ||
private const val CODE_POINT_SIZE = 16 | ||
|
||
/** | ||
* Returns true if the [Buffer] contains human readable text. Uses a small sample | ||
* of code points to detect unicode control characters commonly used in binary file signatures. | ||
*/ | ||
internal val Buffer.isProbablyPlainText | ||
get() = try { | ||
val prefix = Buffer() | ||
val byteCount = min(size, MAX_PREFIX_SIZE) | ||
copyTo(prefix, 0, byteCount) | ||
sequence { while (!prefix.exhausted()) yield(prefix.readUtf8CodePoint()) } | ||
.take(CODE_POINT_SIZE) | ||
.all { codePoint -> codePoint.isPlainTextChar() } | ||
} catch (_: EOFException) { | ||
false // Truncated UTF-8 sequence | ||
} | ||
|
||
private fun Int.isPlainTextChar() = Character.isWhitespace(this) || !Character.isISOControl(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.