| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Network.ByteOrder
Description
Peek and poke functions for network byte order.
Synopsis
- type Buffer = Ptr Word8
- type Offset = Int
- type BufferSize = Int
- data BufferOverrun = BufferOverrun
- poke8 :: Word8 -> Buffer -> Offset -> IO ()
- poke16 :: Word16 -> Buffer -> Offset -> IO ()
- poke24 :: Word32 -> Buffer -> Offset -> IO ()
- poke32 :: Word32 -> Buffer -> Offset -> IO ()
- poke64 :: Word64 -> Buffer -> Offset -> IO ()
- peek8 :: Buffer -> Offset -> IO Word8
- peek16 :: Buffer -> Offset -> IO Word16
- peek24 :: Buffer -> Offset -> IO Word32
- peek32 :: Buffer -> Offset -> IO Word32
- peek64 :: Buffer -> Offset -> IO Word64
- peekByteString :: Buffer -> Int -> IO ByteString
- bytestring8 :: Word8 -> ByteString
- bytestring16 :: Word16 -> ByteString
- bytestring32 :: Word32 -> ByteString
- bytestring64 :: Word64 -> ByteString
- word8 :: ByteString -> Word8
- word16 :: ByteString -> Word16
- word32 :: ByteString -> Word32
- word64 :: ByteString -> Word64
- unsafeWithByteString :: ByteString -> (Buffer -> Offset -> IO a) -> IO a
- copy :: Buffer -> ByteString -> IO Buffer
- unsafeBufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a
- bufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a
- class Readable a where
- read8 :: a -> IO Word8
- readInt8 :: a -> IO Int
- ff :: a -> Offset -> IO ()
- remainingSize :: a -> IO Int
- position :: a -> IO Int
- withCurrentOffSet :: a -> (Buffer -> IO b) -> IO b
- save :: a -> IO ()
- savingSize :: a -> IO Int
- goBack :: a -> IO ()
- data ReadBuffer
- newReadBuffer :: Buffer -> BufferSize -> IO ReadBuffer
- withReadBuffer :: ByteString -> (ReadBuffer -> IO a) -> IO a
- read16 :: Readable a => a -> IO Word16
- read24 :: Readable a => a -> IO Word32
- read32 :: Readable a => a -> IO Word32
- read64 :: Readable a => a -> IO Word64
- extractByteString :: Readable a => a -> Int -> IO ByteString
- extractShortByteString :: Readable a => a -> Int -> IO ShortByteString
- data WriteBuffer = WriteBuffer {}
- newWriteBuffer :: Buffer -> BufferSize -> IO WriteBuffer
- clearWriteBuffer :: WriteBuffer -> IO ()
- withWriteBuffer :: BufferSize -> (WriteBuffer -> IO ()) -> IO ByteString
- withWriteBuffer' :: BufferSize -> (WriteBuffer -> IO a) -> IO (ByteString, a)
- write8 :: WriteBuffer -> Word8 -> IO ()
- write16 :: WriteBuffer -> Word16 -> IO ()
- write24 :: WriteBuffer -> Word32 -> IO ()
- write32 :: WriteBuffer -> Word32 -> IO ()
- write64 :: WriteBuffer -> Word64 -> IO ()
- copyByteString :: WriteBuffer -> ByteString -> IO ()
- copyShortByteString :: WriteBuffer -> ShortByteString -> IO ()
- shiftLastN :: WriteBuffer -> Int -> Int -> IO ()
- toByteString :: WriteBuffer -> IO ByteString
- toShortByteString :: WriteBuffer -> IO ShortByteString
- currentOffset :: WriteBuffer -> IO Buffer
- data Word8
- data Word16
- data Word32
- data Word64
- data ByteString
Types
type BufferSize = Int Source #
Size of a buffer.
data BufferOverrun Source #
Buffer overrun exception.
Constructors
| BufferOverrun | The buffer size is not enough |
Instances
| Eq BufferOverrun Source # | |
Defined in Network.ByteOrder | |
| Exception BufferOverrun Source # | |
Defined in Network.ByteOrder Methods toException :: BufferOverrun -> SomeException fromException :: SomeException -> Maybe BufferOverrun displayException :: BufferOverrun -> String backtraceDesired :: BufferOverrun -> Bool | |
| Show BufferOverrun Source # | |
Defined in Network.ByteOrder Methods showsPrec :: Int -> BufferOverrun -> ShowS show :: BufferOverrun -> String showList :: [BufferOverrun] -> ShowS | |
Poking
poke8 :: Word8 -> Buffer -> Offset -> IO () Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf (poke8 0)>>>unpack buf[0,2,3,4]
poke16 :: Word16 -> Buffer -> Offset -> IO () Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf (poke16 (7*256 + 8))>>>unpack buf[7,8,3,4]
poke24 :: Word32 -> Buffer -> Offset -> IO () Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf (poke24 (6*65536 + 7*256 + 8))>>>unpack buf[6,7,8,4]
poke32 :: Word32 -> Buffer -> Offset -> IO () Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf (poke32 (6*65536 + 7*256 + 8))>>>unpack buf[0,6,7,8]
poke64 :: Word64 -> Buffer -> Offset -> IO () Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>unsafeWithByteString buf (poke64 (6*65536 + 7*256 + 8))>>>unpack buf[0,0,0,0,0,6,7,8]
Peeking
peek8 :: Buffer -> Offset -> IO Word8 Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf peek81
peek16 :: Buffer -> Offset -> IO Word16 Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf peek16258
peek24 :: Buffer -> Offset -> IO Word32 Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf peek2466051
peek32 :: Buffer -> Offset -> IO Word32 Source #
>>>let buf = pack [1,2,3,4]>>>unsafeWithByteString buf peek3216909060
peek64 :: Buffer -> Offset -> IO Word64 Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>unsafeWithByteString buf peek6472623859790382856
peekByteString :: Buffer -> Int -> IO ByteString Source #
Copying the given length of the buffer to a newly created ByteString.
>>>unsafeWithByteString "abcdef" $ \ptr off -> peekByteString (ptr `plusPtr` off) 3"abc"
From Word to ByteString
bytestring8 :: Word8 -> ByteString Source #
>>>let w = 5 :: Word8>>>unpack $ bytestring8 w[5]
bytestring16 :: Word16 -> ByteString Source #
>>>let w = foldl' (\x y -> x * 256 + y) 0 [5,6] :: Word16>>>unpack $ bytestring16 w[5,6]
bytestring32 :: Word32 -> ByteString Source #
>>>let w = foldl' (\x y -> x * 256 + y) 0 [5,6,7,8] :: Word32>>>unpack $ bytestring32 w[5,6,7,8]
bytestring64 :: Word64 -> ByteString Source #
>>>let w = foldl' (\x y -> x * 256 + y) 0 [1,2,3,4,5,6,7,8] :: Word64>>>unpack $ bytestring64 w[1,2,3,4,5,6,7,8]
From ByteString to Word
word8 :: ByteString -> Word8 Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>word8 buf1
BufferOverrun is thrown if the length is not enough.
word16 :: ByteString -> Word16 Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>word16 buf258
word32 :: ByteString -> Word32 Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>word32 buf16909060
word64 :: ByteString -> Word64 Source #
>>>let buf = pack [1,2,3,4,5,6,7,8]>>>word64 buf72623859790382856>>>word64 $ B.take 7 buf*** Exception: BufferOverrun ...
Utilities
unsafeWithByteString :: ByteString -> (Buffer -> Offset -> IO a) -> IO a Source #
Using ByteString as Buffer and call the IO action
of the second argument by passing the start point and the offset
of the ByteString.
Note that if a ByteString is created newly, its offset is 0.
copy :: Buffer -> ByteString -> IO Buffer Source #
Copying the bytestring to the buffer. This function returns the point where the next copy should start.
>>>let buf = "abc" :: ByteString>>>unsafeWithByteString buf $ \ptr _ -> Network.ByteOrder.copy ptr "ABC" >> return buf"ABC"
unsafeBufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a Source #
Converting the part of buffer to ByteString and executing the
action with it.
The ByteString shares the buffer, not copies it.
So, it is valid only in the action and must not be returned,
stored or evaluated lazily after the action.
Use copy to keep its contents.
>>>let buf = "abcdef" :: ByteString>>>unsafeWithByteString buf $ \ptr _-> unsafeBufferIO ptr 2 (return . B.copy)"ab"
Since: 0.1.10
bufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a Source #
Deprecated: Use unsafeBufferIO instead
The same as unsafeBufferIO.
Class to read a buffer
class Readable a where Source #
Methods
read8 :: a -> IO Word8 Source #
Reading one byte as Word8 and ff one byte.
readInt8 :: a -> IO Int Source #
Reading one byte as Int and ff one byte. If buffer overrun occurs, -1 is returned.
ff :: a -> Offset -> IO () Source #
Fast forward the offset pointer.
If buffer overrun occurs, BufferOverrun is thrown.
remainingSize :: a -> IO Int Source #
Returning the length of the remaining
position :: a -> IO Int Source #
Getting the current offset
Since: 0.1.7
withCurrentOffSet :: a -> (Buffer -> IO b) -> IO b Source #
Executing an action on the current offset pointer.
Memorizing the current offset pointer.
savingSize :: a -> IO Int Source #
Getting how many bytes from the saved offset pinter.
Moving the offset point to the saved point.
Instances
| Readable ReadBuffer Source # | |
Defined in Network.ByteOrder Methods read8 :: ReadBuffer -> IO Word8 Source # readInt8 :: ReadBuffer -> IO Int Source # ff :: ReadBuffer -> Offset -> IO () Source # remainingSize :: ReadBuffer -> IO Int Source # position :: ReadBuffer -> IO Int Source # withCurrentOffSet :: ReadBuffer -> (Buffer -> IO b) -> IO b Source # save :: ReadBuffer -> IO () Source # savingSize :: ReadBuffer -> IO Int Source # goBack :: ReadBuffer -> IO () Source # | |
| Readable WriteBuffer Source # | |
Defined in Network.ByteOrder Methods read8 :: WriteBuffer -> IO Word8 Source # readInt8 :: WriteBuffer -> IO Int Source # ff :: WriteBuffer -> Offset -> IO () Source # remainingSize :: WriteBuffer -> IO Int Source # position :: WriteBuffer -> IO Int Source # withCurrentOffSet :: WriteBuffer -> (Buffer -> IO b) -> IO b Source # save :: WriteBuffer -> IO () Source # savingSize :: WriteBuffer -> IO Int Source # goBack :: WriteBuffer -> IO () Source # | |
Reading from buffer
data ReadBuffer Source #
Read only buffer. To ensure that the internal is not modified, this is an abstract data type.
Instances
| Readable ReadBuffer Source # | |
Defined in Network.ByteOrder Methods read8 :: ReadBuffer -> IO Word8 Source # readInt8 :: ReadBuffer -> IO Int Source # ff :: ReadBuffer -> Offset -> IO () Source # remainingSize :: ReadBuffer -> IO Int Source # position :: ReadBuffer -> IO Int Source # withCurrentOffSet :: ReadBuffer -> (Buffer -> IO b) -> IO b Source # save :: ReadBuffer -> IO () Source # savingSize :: ReadBuffer -> IO Int Source # goBack :: ReadBuffer -> IO () Source # | |
newReadBuffer :: Buffer -> BufferSize -> IO ReadBuffer Source #
Creating a read buffer with the given buffer. The size must not be negative. The buffer must be valid while the read buffer is used.
withReadBuffer :: ByteString -> (ReadBuffer -> IO a) -> IO a Source #
Converting ByteString to ReadBuffer and run the action
with it.
The ReadBuffer is valid only in the action.
Neither it nor an action using it must be returned.
read16 :: Readable a => a -> IO Word16 Source #
Reading two bytes as Word16 and ff two bytes.
>>>withReadBuffer "\x0\x1\x2\x3" $ read161
read24 :: Readable a => a -> IO Word32 Source #
Reading three bytes as Word32 and ff three bytes.
>>>withReadBuffer "\x0\x1\x2\x3" $ read24258
read32 :: Readable a => a -> IO Word32 Source #
Reading four bytes as Word32 and ff four bytes.
>>>withReadBuffer "\x0\x1\x2\x3" $ read3266051
extractByteString :: Readable a => a -> Int -> IO ByteString Source #
Extracting ByteString from the current offset.
The contents is copied, not shared.
Its length is specified by the 2nd argument.
If the length is positive, the area after the current pointer is extracted and FF the length finally.
If the length is negative, the area before the current pointer is extracted and does not FF.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 1 >> extractByteString rbuf 2"bc">>>withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 3 >> extractByteString rbuf (-3)"abc">>>withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 3 >> extractByteString rbuf (-4)*** Exception: BufferOverrun ...
extractShortByteString :: Readable a => a -> Int -> IO ShortByteString Source #
Extracting ShortByteString from the current offset.
The contents is copied, not shared.
Its length is specified by the 2nd argument.
If the length is positive, the area after the current pointer is extracted and FF the length finally.
If the length is negative, the area before the current pointer is extracted and does not FF.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 2 >> extractShortByteString rbuf 3"cde"
Writing to buffer
data WriteBuffer Source #
Read and write buffer.
Constructors
| WriteBuffer | |
Instances
| Readable WriteBuffer Source # | |
Defined in Network.ByteOrder Methods read8 :: WriteBuffer -> IO Word8 Source # readInt8 :: WriteBuffer -> IO Int Source # ff :: WriteBuffer -> Offset -> IO () Source # remainingSize :: WriteBuffer -> IO Int Source # position :: WriteBuffer -> IO Int Source # withCurrentOffSet :: WriteBuffer -> (Buffer -> IO b) -> IO b Source # save :: WriteBuffer -> IO () Source # savingSize :: WriteBuffer -> IO Int Source # goBack :: WriteBuffer -> IO () Source # | |
newWriteBuffer :: Buffer -> BufferSize -> IO WriteBuffer Source #
Creating a write buffer with the given buffer. The size must not be negative. The buffer must be valid while the write buffer is used.
clearWriteBuffer :: WriteBuffer -> IO () Source #
Reseting a write buffer.
withWriteBuffer :: BufferSize -> (WriteBuffer -> IO ()) -> IO ByteString Source #
Allocate a temporary buffer and copy the result to ByteString.
withWriteBuffer' :: BufferSize -> (WriteBuffer -> IO a) -> IO (ByteString, a) Source #
Allocate a temporary buffer and copy the result to ByteString with
an additional value.
>>>withWriteBuffer' 1 $ \wbuf -> write8 wbuf 65 >> return 'a'("A",'a')
write8 :: WriteBuffer -> Word8 -> IO () Source #
Write one byte and ff one byte.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 1 $ \wbuf -> write8 wbuf 65"A"
write16 :: WriteBuffer -> Word16 -> IO () Source #
Write two bytes and ff two bytes.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 2 $ \wbuf -> write16 wbuf (65 * 256 + 66)"AB"
write24 :: WriteBuffer -> Word32 -> IO () Source #
Write three bytes and ff three bytes.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 3 $ \wbuf -> write24 wbuf (65 * 256^(2 :: Int) + 66 * 256 + 67)"ABC"
write32 :: WriteBuffer -> Word32 -> IO () Source #
Write four bytes and ff four bytes.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 4 $ \wbuf -> write32 wbuf (65 * 256^(3 :: Int) + 66 * 256^(2 :: Int) + 67 * 256 + 68)"ABCD"
write64 :: WriteBuffer -> Word64 -> IO () Source #
Write eight bytes and ff eight bytes.
If buffer overrun occurs, BufferOverrun is thrown.
copyByteString :: WriteBuffer -> ByteString -> IO () Source #
Copy the content of ByteString and ff its length.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 3 $ \wbuf -> copyByteString wbuf "ABC""ABC"
copyShortByteString :: WriteBuffer -> ShortByteString -> IO () Source #
Copy the content of ShortByteString and ff its length.
If buffer overrun occurs, BufferOverrun is thrown.
>>>withWriteBuffer 5 $ \wbuf -> copyShortByteString wbuf "ABCEF""ABCEF"
shiftLastN :: WriteBuffer -> Int -> Int -> IO () Source #
Shifting the N-bytes area just before the current pointer (the 3rd argument).
If the second argument is positive, shift it to right.
If it is negative, shift it to left.
offset moves as if it is sticky to the area.
>>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "ABCD" >> shiftLastN wbuf 1 3"ABBCD">>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "ABCD" >> shiftLastN wbuf 2 3"ABCBCD">>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "ABCDE" >> shiftLastN wbuf (-2) 3 >> ff wbuf 2"CDEDE">>>B.drop 12 <$> withWriteBuffer 16 (\wbuf -> copyByteString wbuf "ABCD" >> shiftLastN wbuf 12 4)"ABCD">>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf (-4) 2*** Exception: BufferOverrun ...>>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf 1 3*** Exception: BufferOverrun ...>>>withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf 1 (-1)*** Exception: BufferOverrun ...
toByteString :: WriteBuffer -> IO ByteString Source #
Copy the area from start to the current pointer to ByteString.
toShortByteString :: WriteBuffer -> IO ShortByteString Source #
Copy the area from start to the current pointer to ShortByteString.
currentOffset :: WriteBuffer -> IO Buffer Source #
Getting the offset pointer.
Re-exporting
Instances
Instances
Instances
Instances
data ByteString #
Instances
| NFData ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods rnf :: ByteString -> () | |||||
| Monoid ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods mempty :: ByteString mappend :: ByteString -> ByteString -> ByteString mconcat :: [ByteString] -> ByteString | |||||
| Semigroup ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods (<>) :: ByteString -> ByteString -> ByteString sconcat :: NonEmpty ByteString -> ByteString stimes :: Integral b => b -> ByteString -> ByteString | |||||
| Eq ByteString # | |||||
Defined in Data.ByteString.Internal.Type | |||||
| Ord ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods compare :: ByteString -> ByteString -> Ordering (<) :: ByteString -> ByteString -> Bool (<=) :: ByteString -> ByteString -> Bool (>) :: ByteString -> ByteString -> Bool (>=) :: ByteString -> ByteString -> Bool max :: ByteString -> ByteString -> ByteString min :: ByteString -> ByteString -> ByteString | |||||
| Data ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ByteString -> c ByteString gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c ByteString toConstr :: ByteString -> Constr dataTypeOf :: ByteString -> DataType dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c ByteString) dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ByteString) gmapT :: (forall b. Data b => b -> b) -> ByteString -> ByteString gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ByteString -> r gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ByteString -> r gmapQ :: (forall d. Data d => d -> u) -> ByteString -> [u] gmapQi :: Int -> (forall d. Data d => d -> u) -> ByteString -> u gmapM :: Monad m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString | |||||
| IsString ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods fromString :: String -> ByteString | |||||
| IsList ByteString # | |||||
Defined in Data.ByteString.Internal.Type Associated Types
Methods fromList :: [Item ByteString] -> ByteString fromListN :: Int -> [Item ByteString] -> ByteString toList :: ByteString -> [Item ByteString] | |||||
| Read ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods readsPrec :: Int -> ReadS ByteString readList :: ReadS [ByteString] readPrec :: ReadPrec ByteString readListPrec :: ReadPrec [ByteString] | |||||
| Show ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods showsPrec :: Int -> ByteString -> ShowS show :: ByteString -> String showList :: [ByteString] -> ShowS | |||||
| Lift ByteString # | |||||
Defined in Data.ByteString.Internal.Type Methods lift :: Quote m => ByteString -> m Exp liftTyped :: forall (m :: Type -> Type). Quote m => ByteString -> Code m ByteString | |||||
| type Item ByteString # | |||||
Defined in Data.ByteString.Internal.Type | |||||