Many types of String (ByteString)
Asked Answered
Y

3

17

I wish to compress my application's network traffic.

According to the (latest?) "Haskell Popularity Rankings", zlib seems to be a pretty popular solution. zlib's interface uses ByteStrings:

compress :: ByteString -> ByteString
decompress :: ByteString -> ByteString

I am using regular Strings, which are also the data types used by read, show, and Network.Socket:

sendTo :: Socket -> String -> SockAddr -> IO Int
recvFrom :: Socket -> Int -> IO (String, Int, SockAddr)

So to compress my strings, I need some way to convert a String to a ByteString and vice-versa. With hoogle's help, I found:

Data.ByteString.Char8 pack :: String -> ByteString

Trying to use it:

Prelude Codec.Compression.Zlib Data.ByteString.Char8> compress (pack "boo")

<interactive>:1:10:
    Couldn't match expected type `Data.ByteString.Lazy.Internal.ByteString'
           against inferred type `ByteString'
    In the first argument of `compress', namely `(pack "boo")'
    In the expression: compress (pack "boo")
In the definition of `it': it = compress (pack "boo")

Fails, because (?) there are different types of ByteString ?

So basically:

  • Are there several types of ByteString? What types, and why?
  • What's "the" way to convert Strings to ByteStrings?

Btw, I found that it does work with Data.ByteString.Lazy.Char8's ByteString, but I'm still intrigued.

Yourself answered 20/9, 2009 at 19:10 Comment(0)
T
10

There are two kinds of bytestrings: strict (defined in Data.Bytestring.Internal) and lazy (defined in Data.Bytestring.Lazy.Internal). zlib uses lazy bytestrings, as you've discovered.

Toowoomba answered 20/9, 2009 at 20:58 Comment(1)
No. Char8 modules just give a different interface to the same bytestrings.Toowoomba
S
8

The function you're looking for is:

import Data.ByteString as BS
import Data.ByteString.Lazy as LBS

lazyToStrictBS :: LBS.ByteString -> BS.ByteString
lazyToStrictBS x = BS.concat $ LBS.toChunks x

I expect it can be written more concisely without the x. (i.e. point-free, but I'm new to Haskell.)

Sweatt answered 15/6, 2012 at 14:53 Comment(1)
like lazyToStrictBS = BS.concat . LBS.toChunksNutt
S
6

A more efficient mechanism might be to switch to a full bytestring-based layer:

  • network.bytestring for bytestring sockets
  • lazy bytestrings for compressoin
  • binary of bytestring-show to replace Show/Read
Succory answered 20/9, 2009 at 19:41 Comment(4)
@dons: it appears that I will need to use Network.Socket.ByteString.Lazy for compatibility with zlib, but unlike Network.Socket.ByteString, it doesn't implement sendTo and recvFrom, making it unsuitable for me.Yourself
I don't think it will be too hard for you to stream chunks of lazy bytestrings into a socket taking individual ones. It's essentially mapM_ send . fromChunksSuccory
@dons: when using UDP it will actually make a difference if you send your packet in whole or in chunks. but are you also saying that conversion between lazy and strict bytestring is easy? that would be useful to know..Yourself
@Yourself - the functions toChunks and fromChunks in Data.ByteString.Lazy convert between strict and lazy bytestrings.Angry

© 2022 - 2024 — McMap. All rights reserved.