2015-10-02 1 views
2

Étant donné un exemple de fichier texte, comment utiliser Akka ByteStrings et le convertir en texte brut ou lancer une recherche sur le ByteString?Comment puis-je déterminer si Akka bytestring contient une sous-chaîne donnée?

  val file = new File("sample.txt") 
      val fileSource = SynchronousFileSource(file, 4096) 
      val messageStream = fileSource.map(chunk => sendMessage(chunk.toString())) 
      messageStream.to(Sink.foreach(println(_))).run 

Le « toString() » fonctionnalité ci-dessus crache littéralement une chaîne contenant le texte « ByteString », suivi d'octets représentés comme des entiers. Par exemple:

  chunk.toString() ==> "ByteString(111, 112, 119, 111)" 

Répondre

6

Vous pouvez utiliser containsSlice pour trouver sub ByteString.

scala> import akka.util.ByteString; 
import akka.util.ByteString 

scala> val target = ByteString("hello world"); 
target: akka.util.ByteString = ByteString(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100) 

scala> val sub = ByteString("world") 
sub: akka.util.ByteString = ByteString(119, 111, 114, 108, 100) 

scala> target.containsSlice(sub) 
res0: Boolean = true 

Si vous voulez convertir akka.util.ByteString-String, vous pouvez utiliser decodeString

scala> ByteString("hello").decodeString("UTF-8") 
res3: String = hello 

Voir la doc pour plus de détails: http://doc.akka.io/api/akka/2.3.13/index.html#akka.util.ByteString

+1

parfait, trouve juste dans docs et est revenu au poste retour .. lo et voici, déjà résolu! Merci. –