Text and charsets
The text flows allow to translate a stream of text data according to the used character sets. It supports conversion between ByteString and String, as well as conversion of the character set in binary text data in the form of ByteStrings.
The main use case for these flows is the transcoding of text read from a source with a certain character set, which may not be usable with other flows or sinks. For example may CSV data arrive in UTF-16 encoding, but the Alpakka CSV parser does only support UTF-8.
[+] Show project infoProject Info: Alpakka Text | |
---|---|
Artifact | com.lightbend.akka
akka-stream-alpakka-text
9.0.0
|
JDK versions | Eclipse Temurin JDK 11 Eclipse Temurin JDK 17 |
Scala versions | 2.13.12, 3.3.3 |
JPMS module name | akka.stream.alpakka.text |
License | |
Readiness level |
Since 0.20, 2018-07-04
|
Home page | https://doc.akka.io/libraries/alpakka/current |
API documentation | |
Forums | |
Release notes | GitHub releases |
Issues | Github issues |
Sources | https://github.com/akka/alpakka |
Artifacts
The Akka dependencies are available from Akka’s library repository. To access them there, you need to configure the URL for this repository.
Additionally, add the dependencies as below.
- sbt
val AkkaVersion = "2.10.0" libraryDependencies ++= Seq( "com.lightbend.akka" %% "akka-stream-alpakka-text" % "9.0.0", "com.typesafe.akka" %% "akka-stream" % AkkaVersion )
- Maven
- Gradle
The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.
- Direct dependencies
Organization Artifact Version com.typesafe.akka akka-stream_2.13 2.10.0 org.scala-lang scala-library 2.13.12 - Dependency tree
Text transcoding
The text transcoding flow converts incoming binary text data (ByteString) to binary text data of another character encoding.
The flow fails with an UnmappableCharacterException
, if a character is not representable in the targeted character set.
- Scala
- Java
-
source
Source<ByteString, ?> byteStringSource = // ... byteStringSource .via(TextFlow.transcoding(StandardCharsets.UTF_16, StandardCharsets.UTF_8)) .runWith(FileIO.toPath(targetFile), system);
Text encoding
The text encoding flow converts incoming Strings to binary text data (ByteString) with the given character encoding.
The flow fails with an UnmappableCharacterException
, if a character is not representable in the targeted character set.
- Scala
- Java
-
source
import akka.stream.alpakka.testkit.javadsl.LogCapturingJunit4; import akka.stream.alpakka.text.javadsl.TextFlow; import akka.stream.IOResult; import akka.stream.javadsl.FileIO; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.util.ByteString; import java.nio.charset.StandardCharsets; Source<String, ?> stringSource = // ... stringSource .via(TextFlow.encoding(StandardCharsets.US_ASCII)) .intersperse(ByteString.fromString("\n")) .runWith(FileIO.toPath(targetFile), system);
Text decoding
The text decoding flow converts incoming ByteStrings to Strings using the given character encoding.