yairchu
yairchu

Reputation: 24754

Many types of String (ByteString)

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:

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

Upvotes: 17

Views: 4456

Answers (3)

fadedbee
fadedbee

Reputation: 44739

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.)

Upvotes: 8

Alexey Romanov
Alexey Romanov

Reputation: 170713

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.

Upvotes: 10

Don Stewart
Don Stewart

Reputation: 137937

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

Upvotes: 6

Related Questions