Skip to content

Commit

Permalink
KAFKA-9408: Use StandardCharsets.UTF-8 instead of "UTF-8" (#7940)
Browse files Browse the repository at this point in the history
Reviewers: Ron Dagostino <rdagostino@confluent.io>, Ismael Juma <ismael@juma.me.uk>
  • Loading branch information
belugabehr committed Jan 30, 2020
1 parent 57cef76 commit 4357656
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
9 changes: 4 additions & 5 deletions core/src/main/scala/kafka/api/ApiUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package kafka.api

import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets

import org.apache.kafka.common.KafkaException

/**
* Helper functions specific to parsing or serializing requests and responses
*/
object ApiUtils {

val ProtocolEncoding = "UTF-8"

/**
* Read size prefixed string where the size is stored as a 2 byte short.
Expand All @@ -37,7 +36,7 @@ object ApiUtils {
return null
val bytes = new Array[Byte](size)
buffer.get(bytes)
new String(bytes, ProtocolEncoding)
new String(bytes, StandardCharsets.UTF_8)
}

/**
Expand All @@ -49,7 +48,7 @@ object ApiUtils {
if(string == null) {
buffer.putShort(-1)
} else {
val encodedString = string.getBytes(ProtocolEncoding)
val encodedString = string.getBytes(StandardCharsets.UTF_8)
if(encodedString.length > Short.MaxValue) {
throw new KafkaException("String exceeds the maximum size of " + Short.MaxValue + ".")
} else {
Expand All @@ -67,7 +66,7 @@ object ApiUtils {
if(string == null) {
2
} else {
val encodedString = string.getBytes(ProtocolEncoding)
val encodedString = string.getBytes(StandardCharsets.UTF_8)
if(encodedString.length > Short.MaxValue) {
throw new KafkaException("String exceeds the maximum size of " + Short.MaxValue + ".")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package kafka.api

import java.lang.{Long => JLong}
import java.nio.charset.StandardCharsets
import java.time.Duration
import java.util.{Optional, Properties}
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -271,20 +272,20 @@ class TransactionsTest extends KafkaServerTestHarness {
shouldCommit = !shouldCommit

records.foreach { record =>
val key = new String(record.key(), "UTF-8")
val value = new String(record.value(), "UTF-8")
val key = new String(record.key(), StandardCharsets.UTF_8)
val value = new String(record.value(), StandardCharsets.UTF_8)
producer.send(TestUtils.producerRecordWithExpectedTransactionStatus(topic2, key, value, willBeCommitted = shouldCommit))
}

commit(producer, consumerGroupId, consumer)
if (shouldCommit) {
producer.commitTransaction()
recordsProcessed += records.size
debug(s"committed transaction.. Last committed record: ${new String(records.last.value(), "UTF-8")}. Num " +
debug(s"committed transaction.. Last committed record: ${new String(records.last.value(), StandardCharsets.UTF_8)}. Num " +
s"records written to $topic2: $recordsProcessed")
} else {
producer.abortTransaction()
debug(s"aborted transaction Last committed record: ${new String(records.last.value(), "UTF-8")}. Num " +
debug(s"aborted transaction Last committed record: ${new String(records.last.value(), StandardCharsets.UTF_8)}. Num " +
s"records written to $topic2: $recordsProcessed")
TestUtils.resetToCommittedPositions(consumer)
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/scala/unit/kafka/log/LogCleanerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kafka.log

import java.io.{File, RandomAccessFile}
import java.nio._
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import java.util.Properties
import java.util.concurrent.{CountDownLatch, TimeUnit}
Expand Down Expand Up @@ -1730,7 +1731,7 @@ class FakeOffsetMap(val slots: Int) extends OffsetMap {
var lastOffset = -1L

private def keyFor(key: ByteBuffer) =
new String(Utils.readBytes(key.duplicate), "UTF-8")
new String(Utils.readBytes(key.duplicate), StandardCharsets.UTF_8)

override def put(key: ByteBuffer, offset: Long): Unit = {
lastOffset = offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.io._
import java.net._
import java.nio.ByteBuffer
import java.nio.channels.{SelectionKey, SocketChannel}
import java.nio.charset.StandardCharsets
import java.util
import java.util.concurrent.{CompletableFuture, ConcurrentLinkedQueue, Executors, TimeUnit}
import java.util.{HashMap, Properties, Random}
Expand Down Expand Up @@ -920,7 +921,7 @@ class SocketServerTest {
receiveResponse(socket)

// now send credentials
val authBytes = "admin\u0000admin\u0000admin-secret".getBytes("UTF-8")
val authBytes = "admin\u0000admin\u0000admin-secret".getBytes(StandardCharsets.UTF_8)
if (leverageKip152SaslAuthenticateRequest) {
// send credentials within a SaslAuthenticateRequest
val saslAuthenticateRequest = new SaslAuthenticateRequest.Builder(new SaslAuthenticateRequestData()
Expand Down

0 comments on commit 4357656

Please sign in to comment.