{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream' is implemented by @/GOutputStreams/@ that
can be polled for readiness to write. This can be used when
interfacing with a non-GIO API that expects
UNIX-file-descriptor-style asynchronous I\/O rather than GIO-style.

/Since: 2.28/
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.Gio.Interfaces.PollableOutputStream
    (

-- * Exported types
    PollableOutputStream(..)                ,
    noPollableOutputStream                  ,
    IsPollableOutputStream                  ,
    toPollableOutputStream                  ,


 -- * Methods
-- ** canPoll #method:canPoll#

#if ENABLE_OVERLOADING
    PollableOutputStreamCanPollMethodInfo   ,
#endif
    pollableOutputStreamCanPoll             ,


-- ** createSource #method:createSource#

#if ENABLE_OVERLOADING
    PollableOutputStreamCreateSourceMethodInfo,
#endif
    pollableOutputStreamCreateSource        ,


-- ** isWritable #method:isWritable#

#if ENABLE_OVERLOADING
    PollableOutputStreamIsWritableMethodInfo,
#endif
    pollableOutputStreamIsWritable          ,


-- ** writeNonblocking #method:writeNonblocking#

#if ENABLE_OVERLOADING
    PollableOutputStreamWriteNonblockingMethodInfo,
#endif
    pollableOutputStreamWriteNonblocking    ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP

import qualified GI.GLib.Structs.Source as GLib.Source
import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Gio.Objects.Cancellable as Gio.Cancellable
import {-# SOURCE #-} qualified GI.Gio.Objects.OutputStream as Gio.OutputStream

-- interface PollableOutputStream 
-- | Memory-managed wrapper type.
newtype PollableOutputStream = PollableOutputStream (ManagedPtr PollableOutputStream)
-- | A convenience alias for `Nothing` :: `Maybe` `PollableOutputStream`.
noPollableOutputStream :: Maybe PollableOutputStream
noPollableOutputStream = Nothing

#if ENABLE_OVERLOADING
type instance O.SignalList PollableOutputStream = PollableOutputStreamSignalList
type PollableOutputStreamSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

foreign import ccall "g_pollable_output_stream_get_type"
    c_g_pollable_output_stream_get_type :: IO GType

instance GObject PollableOutputStream where
    gobjectType _ = c_g_pollable_output_stream_get_type


-- | Type class for types which can be safely cast to `PollableOutputStream`, for instance with `toPollableOutputStream`.
class GObject o => IsPollableOutputStream o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError PollableOutputStream a) =>
    IsPollableOutputStream a
#endif
instance IsPollableOutputStream PollableOutputStream
instance Gio.OutputStream.IsOutputStream PollableOutputStream
instance GObject.Object.IsObject PollableOutputStream

-- | Cast to `PollableOutputStream`, for types for which this is known to be safe. For general casts, use `Data.GI.Base.ManagedPtr.castTo`.
toPollableOutputStream :: (MonadIO m, IsPollableOutputStream o) => o -> m PollableOutputStream
toPollableOutputStream = liftIO . unsafeCastTo PollableOutputStream

#if ENABLE_OVERLOADING
instance O.HasAttributeList PollableOutputStream
type instance O.AttributeList PollableOutputStream = PollableOutputStreamAttributeList
type PollableOutputStreamAttributeList = ('[ ] :: [(Symbol, *)])
#endif

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type family ResolvePollableOutputStreamMethod (t :: Symbol) (o :: *) :: * where
    ResolvePollableOutputStreamMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolvePollableOutputStreamMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolvePollableOutputStreamMethod "canPoll" o = PollableOutputStreamCanPollMethodInfo
    ResolvePollableOutputStreamMethod "clearPending" o = Gio.OutputStream.OutputStreamClearPendingMethodInfo
    ResolvePollableOutputStreamMethod "close" o = Gio.OutputStream.OutputStreamCloseMethodInfo
    ResolvePollableOutputStreamMethod "closeAsync" o = Gio.OutputStream.OutputStreamCloseAsyncMethodInfo
    ResolvePollableOutputStreamMethod "closeFinish" o = Gio.OutputStream.OutputStreamCloseFinishMethodInfo
    ResolvePollableOutputStreamMethod "createSource" o = PollableOutputStreamCreateSourceMethodInfo
    ResolvePollableOutputStreamMethod "flush" o = Gio.OutputStream.OutputStreamFlushMethodInfo
    ResolvePollableOutputStreamMethod "flushAsync" o = Gio.OutputStream.OutputStreamFlushAsyncMethodInfo
    ResolvePollableOutputStreamMethod "flushFinish" o = Gio.OutputStream.OutputStreamFlushFinishMethodInfo
    ResolvePollableOutputStreamMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolvePollableOutputStreamMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolvePollableOutputStreamMethod "getv" o = GObject.Object.ObjectGetvMethodInfo
    ResolvePollableOutputStreamMethod "hasPending" o = Gio.OutputStream.OutputStreamHasPendingMethodInfo
    ResolvePollableOutputStreamMethod "isClosed" o = Gio.OutputStream.OutputStreamIsClosedMethodInfo
    ResolvePollableOutputStreamMethod "isClosing" o = Gio.OutputStream.OutputStreamIsClosingMethodInfo
    ResolvePollableOutputStreamMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolvePollableOutputStreamMethod "isWritable" o = PollableOutputStreamIsWritableMethodInfo
    ResolvePollableOutputStreamMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolvePollableOutputStreamMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolvePollableOutputStreamMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolvePollableOutputStreamMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolvePollableOutputStreamMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolvePollableOutputStreamMethod "splice" o = Gio.OutputStream.OutputStreamSpliceMethodInfo
    ResolvePollableOutputStreamMethod "spliceAsync" o = Gio.OutputStream.OutputStreamSpliceAsyncMethodInfo
    ResolvePollableOutputStreamMethod "spliceFinish" o = Gio.OutputStream.OutputStreamSpliceFinishMethodInfo
    ResolvePollableOutputStreamMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolvePollableOutputStreamMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolvePollableOutputStreamMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolvePollableOutputStreamMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolvePollableOutputStreamMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolvePollableOutputStreamMethod "write" o = Gio.OutputStream.OutputStreamWriteMethodInfo
    ResolvePollableOutputStreamMethod "writeAll" o = Gio.OutputStream.OutputStreamWriteAllMethodInfo
    ResolvePollableOutputStreamMethod "writeAllAsync" o = Gio.OutputStream.OutputStreamWriteAllAsyncMethodInfo
    ResolvePollableOutputStreamMethod "writeAllFinish" o = Gio.OutputStream.OutputStreamWriteAllFinishMethodInfo
    ResolvePollableOutputStreamMethod "writeAsync" o = Gio.OutputStream.OutputStreamWriteAsyncMethodInfo
    ResolvePollableOutputStreamMethod "writeBytes" o = Gio.OutputStream.OutputStreamWriteBytesMethodInfo
    ResolvePollableOutputStreamMethod "writeBytesAsync" o = Gio.OutputStream.OutputStreamWriteBytesAsyncMethodInfo
    ResolvePollableOutputStreamMethod "writeBytesFinish" o = Gio.OutputStream.OutputStreamWriteBytesFinishMethodInfo
    ResolvePollableOutputStreamMethod "writeFinish" o = Gio.OutputStream.OutputStreamWriteFinishMethodInfo
    ResolvePollableOutputStreamMethod "writeNonblocking" o = PollableOutputStreamWriteNonblockingMethodInfo
    ResolvePollableOutputStreamMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolvePollableOutputStreamMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolvePollableOutputStreamMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolvePollableOutputStreamMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolvePollableOutputStreamMethod "setPending" o = Gio.OutputStream.OutputStreamSetPendingMethodInfo
    ResolvePollableOutputStreamMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolvePollableOutputStreamMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePollableOutputStreamMethod t PollableOutputStream, O.MethodInfo info PollableOutputStream p) => O.IsLabelProxy t (PollableOutputStream -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolvePollableOutputStreamMethod t PollableOutputStream, O.MethodInfo info PollableOutputStream p) => O.IsLabel t (PollableOutputStream -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif
#endif

#endif

-- method PollableOutputStream::can_poll
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "PollableOutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GPollableOutputStream.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_can_poll" g_pollable_output_stream_can_poll ::
    Ptr PollableOutputStream ->             -- stream : TInterface (Name {namespace = "Gio", name = "PollableOutputStream"})
    IO CInt

{- |
Checks if /@stream@/ is actually pollable. Some classes may implement
'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream' but have only certain instances of that
class be pollable. If this method returns 'False', then the behavior
of other 'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream' methods is undefined.

For any given stream, the value returned by this method is constant;
a stream cannot switch from pollable to non-pollable or vice versa.

/Since: 2.28/
-}
pollableOutputStreamCanPoll ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableOutputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream'. -}
    -> m Bool
    {- ^ __Returns:__ 'True' if /@stream@/ is pollable, 'False' if not. -}
pollableOutputStreamCanPoll stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_pollable_output_stream_can_poll stream'
    let result' = (/= 0) result
    touchManagedPtr stream
    return result'

#if ENABLE_OVERLOADING
data PollableOutputStreamCanPollMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsPollableOutputStream a) => O.MethodInfo PollableOutputStreamCanPollMethodInfo a signature where
    overloadedMethod _ = pollableOutputStreamCanPoll

#endif

-- method PollableOutputStream::create_source
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "PollableOutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GPollableOutputStream.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "cancellable", argType = TInterface (Name {namespace = "Gio", name = "Cancellable"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GCancellable, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "Source"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_create_source" g_pollable_output_stream_create_source ::
    Ptr PollableOutputStream ->             -- stream : TInterface (Name {namespace = "Gio", name = "PollableOutputStream"})
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    IO (Ptr GLib.Source.Source)

{- |
Creates a 'GI.GLib.Structs.Source.Source' that triggers when /@stream@/ can be written, or
/@cancellable@/ is triggered or an error occurs. The callback on the
source is of the 'GI.Gio.Callbacks.PollableSourceFunc' type.

As with 'GI.Gio.Interfaces.PollableOutputStream.pollableOutputStreamIsWritable', it is possible that
the stream may not actually be writable even after the source
triggers, so you should use 'GI.Gio.Interfaces.PollableOutputStream.pollableOutputStreamWriteNonblocking'
rather than 'GI.Gio.Objects.OutputStream.outputStreamWrite' from the callback.

/Since: 2.28/
-}
pollableOutputStreamCreateSource ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableOutputStream a, Gio.Cancellable.IsCancellable b) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream'. -}
    -> Maybe (b)
    {- ^ /@cancellable@/: a 'GI.Gio.Objects.Cancellable.Cancellable', or 'Nothing' -}
    -> m GLib.Source.Source
    {- ^ __Returns:__ a new 'GI.GLib.Structs.Source.Source' -}
pollableOutputStreamCreateSource stream cancellable = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            jCancellable' <- unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    result <- g_pollable_output_stream_create_source stream' maybeCancellable
    checkUnexpectedReturnNULL "pollableOutputStreamCreateSource" result
    result' <- (wrapBoxed GLib.Source.Source) result
    touchManagedPtr stream
    whenJust cancellable touchManagedPtr
    return result'

#if ENABLE_OVERLOADING
data PollableOutputStreamCreateSourceMethodInfo
instance (signature ~ (Maybe (b) -> m GLib.Source.Source), MonadIO m, IsPollableOutputStream a, Gio.Cancellable.IsCancellable b) => O.MethodInfo PollableOutputStreamCreateSourceMethodInfo a signature where
    overloadedMethod _ = pollableOutputStreamCreateSource

#endif

-- method PollableOutputStream::is_writable
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "PollableOutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GPollableOutputStream.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_is_writable" g_pollable_output_stream_is_writable ::
    Ptr PollableOutputStream ->             -- stream : TInterface (Name {namespace = "Gio", name = "PollableOutputStream"})
    IO CInt

{- |
Checks if /@stream@/ can be written.

Note that some stream types may not be able to implement this 100%
reliably, and it is possible that a call to 'GI.Gio.Objects.OutputStream.outputStreamWrite'
after this returns 'True' would still block. To guarantee
non-blocking behavior, you should always use
'GI.Gio.Interfaces.PollableOutputStream.pollableOutputStreamWriteNonblocking', which will return a
'GI.Gio.Enums.IOErrorEnumWouldBlock' error rather than blocking.

/Since: 2.28/
-}
pollableOutputStreamIsWritable ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableOutputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream'. -}
    -> m Bool
    {- ^ __Returns:__ 'True' if /@stream@/ is writable, 'False' if not. If an error
  has occurred on /@stream@/, this will result in
  'GI.Gio.Interfaces.PollableOutputStream.pollableOutputStreamIsWritable' returning 'True', and the
  next attempt to write will return the error. -}
pollableOutputStreamIsWritable stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_pollable_output_stream_is_writable stream'
    let result' = (/= 0) result
    touchManagedPtr stream
    return result'

#if ENABLE_OVERLOADING
data PollableOutputStreamIsWritableMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsPollableOutputStream a) => O.MethodInfo PollableOutputStreamIsWritableMethodInfo a signature where
    overloadedMethod _ = pollableOutputStreamIsWritable

#endif

-- method PollableOutputStream::write_nonblocking
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "PollableOutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GPollableOutputStream", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "buffer", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a buffer to write\n    data from", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the number of bytes you want to write", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "cancellable", argType = TInterface (Name {namespace = "Gio", name = "Cancellable"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GCancellable, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the number of bytes you want to write", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TBasicType TInt64)
-- throws : True
-- Skip return : False

foreign import ccall "g_pollable_output_stream_write_nonblocking" g_pollable_output_stream_write_nonblocking ::
    Ptr PollableOutputStream ->             -- stream : TInterface (Name {namespace = "Gio", name = "PollableOutputStream"})
    Ptr Word8 ->                            -- buffer : TCArray False (-1) 2 (TBasicType TUInt8)
    Word64 ->                               -- count : TBasicType TUInt64
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    Ptr (Ptr GError) ->                     -- error
    IO Int64

{- |
Attempts to write up to /@count@/ bytes from /@buffer@/ to /@stream@/, as
with 'GI.Gio.Objects.OutputStream.outputStreamWrite'. If /@stream@/ is not currently writable,
this will immediately return 'GI.Gio.Enums.IOErrorEnumWouldBlock', and you can
use 'GI.Gio.Interfaces.PollableOutputStream.pollableOutputStreamCreateSource' to create a 'GI.GLib.Structs.Source.Source'
that will be triggered when /@stream@/ is writable.

Note that since this method never blocks, you cannot actually
use /@cancellable@/ to cancel it. However, it will return an error
if /@cancellable@/ has already been cancelled when you call, which
may happen if you call this method after a source triggers due
to having been cancelled.

Also note that if 'GI.Gio.Enums.IOErrorEnumWouldBlock' is returned some underlying
transports like D\/TLS require that you send the same /@buffer@/ and /@count@/.
-}
pollableOutputStreamWriteNonblocking ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableOutputStream a, Gio.Cancellable.IsCancellable b) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Interfaces.PollableOutputStream.PollableOutputStream' -}
    -> Maybe (ByteString)
    {- ^ /@buffer@/: a buffer to write
    data from -}
    -> Maybe (b)
    {- ^ /@cancellable@/: a 'GI.Gio.Objects.Cancellable.Cancellable', or 'Nothing' -}
    -> m Int64
    {- ^ __Returns:__ the number of bytes written, or -1 on error (including
  'GI.Gio.Enums.IOErrorEnumWouldBlock'). /(Can throw 'Data.GI.Base.GError.GError')/ -}
pollableOutputStreamWriteNonblocking stream buffer cancellable = liftIO $ do
    let count = case buffer of
            Nothing -> 0
            Just jBuffer -> fromIntegral $ B.length jBuffer
    stream' <- unsafeManagedPtrCastPtr stream
    maybeBuffer <- case buffer of
        Nothing -> return nullPtr
        Just jBuffer -> do
            jBuffer' <- packByteString jBuffer
            return jBuffer'
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            jCancellable' <- unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    onException (do
        result <- propagateGError $ g_pollable_output_stream_write_nonblocking stream' maybeBuffer count maybeCancellable
        touchManagedPtr stream
        whenJust cancellable touchManagedPtr
        freeMem maybeBuffer
        return result
     ) (do
        freeMem maybeBuffer
     )

#if ENABLE_OVERLOADING
data PollableOutputStreamWriteNonblockingMethodInfo
instance (signature ~ (Maybe (ByteString) -> Maybe (b) -> m Int64), MonadIO m, IsPollableOutputStream a, Gio.Cancellable.IsCancellable b) => O.MethodInfo PollableOutputStreamWriteNonblockingMethodInfo a signature where
    overloadedMethod _ = pollableOutputStreamWriteNonblocking

#endif