All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bugvm.apple.corefoundation.CFReadStream Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
/*
 * Copyright (C) 2013-2015 RoboVM AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bugvm.apple.corefoundation;

/**/
import java.io.*;
import java.nio.*;
import java.util.*;
import com.bugvm.objc.*;
import com.bugvm.objc.annotation.*;
import com.bugvm.objc.block.*;
import com.bugvm.rt.*;
import com.bugvm.rt.annotation.*;
import com.bugvm.rt.bro.*;
import com.bugvm.rt.bro.annotation.*;
import com.bugvm.rt.bro.ptr.*;
import com.bugvm.apple.foundation.*;
import com.bugvm.apple.dispatch.*;
import com.bugvm.apple.coreservices.*;
import com.bugvm.apple.coremedia.*;
import com.bugvm.apple.uikit.*;
import com.bugvm.apple.coretext.*;
/**/

/**/
/**/
/**/@Library("CoreFoundation")/**/
/**/public/**/ class /**/CFReadStream/**/ 
    extends /**/CFType/**/ 
    /**//**/ {

    public interface ClientCallback {
        void invoke(CFReadStream stream, CFStreamEventType eventType);
    }
    
    private static final java.util.concurrent.atomic.AtomicLong refconId = new java.util.concurrent.atomic.AtomicLong();
    private static final LongMap clientCallbacks = new LongMap<>();
    private static final java.lang.reflect.Method cbClient;
    
    static {
        try {
            cbClient = CFWriteStream.class.getDeclaredMethod("cbClient", CFReadStream.class, CFStreamEventType.class, long.class);
        } catch (Throwable e) {
            throw new Error(e);
        }
    }
    
    /**/public static class CFReadStreamPtr extends Ptr {}/**/
    /**/static { Bro.bind(CFReadStream.class); }/**/
    /**//**/
    /**/
    protected CFReadStream() {}
    /**/
    /**//**/
    /**//**/
    private long localRefconId;
    
    @Callback
    private static void cbClient(CFReadStream stream, CFStreamEventType eventType, @Pointer long refcon) {
        ClientCallback callback = null;
        synchronized (clientCallbacks) {
            callback = clientCallbacks.get(refcon);
        }
        callback.invoke(stream, eventType);
    }
    
    public static CFReadStream create(byte[] bytes) {
        if (bytes == null) {
            throw new NullPointerException("bytes");
        }
        return create(null, VM.getArrayValuesAddress(bytes), bytes.length, null);
    }
    public static CFReadStream create(ByteBuffer bytes) {
        if (bytes == null) {
            throw new NullPointerException("bytes");
        }
        long handle = CFData.getEffectiveAddress(bytes) + bytes.position();
        return create(null, handle, bytes.remaining(), null);
    }
    public static CFReadStream create(CFURL fileURL) {
        return create(null, fileURL);
    }
    
    public long read(BytePtr buffer, long len) {
        return read(buffer.getHandle(), len);
    }
    public long read(ByteBuffer bytes) {
        long handle = CFData.getEffectiveAddress(bytes) + bytes.position();
        return read(handle, bytes.remaining());
    }
    public long read(byte[] bytes) {
        return read(bytes, 0, bytes.length);
    }
    public long read(byte[] bytes, int offset, int length) {
        CFMutableData.checkOffsetAndCount(bytes.length, offset, length);
        if (length == 0) {
            return 0;
        }
        return read(VM.getArrayValuesAddress(bytes) + offset, length);
    }
    
    public boolean setClientCallback(CFStreamEventType streamEvents, ClientCallback callback) {
        localRefconId = refconId.getAndIncrement();
        CFStreamClientContext context = new CFStreamClientContext();
        context.setInfo(localRefconId);
        boolean result = setClient(streamEvents, new FunctionPtr(cbClient), context);
        if (result) {
            synchronized (clientCallbacks) {
                clientCallbacks.put(localRefconId, callback);
            }
        }
        return result;
    }
    
    public CFType getProperty(CFStreamProperty property) {
        return getProperty(property.value());
    }
    @WeaklyLinked
    public CFType getProperty(CFSocketStreamProperty property) {
        return getProperty(property.value());
    }
    @WeaklyLinked
    public CFType getProperty(CFFTPStreamProperty property) {
        return getProperty(property.value());
    }
    public boolean setProperty(CFStreamProperty property, CFType propertyValue) {
        return setProperty(property.value(), propertyValue);
    }
    @WeaklyLinked
    public boolean setProperty(CFSocketStreamProperty property, CFType propertyValue) {
        return setProperty(property.value(), propertyValue);
    }
    @WeaklyLinked
    public boolean setProperty(CFFTPStreamProperty property, CFType propertyValue) {
        return setProperty(property.value(), propertyValue);
    }
    
    public void scheduleInRunLoop(CFRunLoop runLoop, CFRunLoopMode runLoopMode) {
        scheduleInRunLoop(runLoop, runLoopMode.value().toString());
    }
    public void unscheduleFromRunLoop(CFRunLoop runLoop, CFRunLoopMode runLoopMode) {
        unscheduleFromRunLoop(runLoop, runLoopMode.value().toString());
    }
    /**/
    @Bridge(symbol="CFReadStreamGetTypeID", optional=true)
    public static native @MachineSizedUInt long getClassTypeID();
    @Bridge(symbol="CFReadStreamCreateWithBytesNoCopy", optional=true)
    private static native @com.bugvm.rt.bro.annotation.Marshaler(CFType.NoRetainMarshaler.class) CFReadStream create(CFAllocator alloc, @Pointer long bytes, @MachineSizedSInt long length, CFAllocator bytesDeallocator);
    @Bridge(symbol="CFReadStreamCreateWithFile", optional=true)
    public static native @com.bugvm.rt.bro.annotation.Marshaler(CFType.NoRetainMarshaler.class) CFReadStream create(CFAllocator alloc, CFURL fileURL);
    @Bridge(symbol="CFReadStreamGetStatus", optional=true)
    public native CFStreamStatus getStatus();
    /**
     * @since Available in iOS 2.0 and later.
     */
    @Bridge(symbol="CFReadStreamCopyError", optional=true)
    public native @com.bugvm.rt.bro.annotation.Marshaler(CFType.NoRetainMarshaler.class) CFError getError();
    @Bridge(symbol="CFReadStreamOpen", optional=true)
    public native boolean openStream();
    @Bridge(symbol="CFReadStreamClose", optional=true)
    public native void closeStream();
    @Bridge(symbol="CFReadStreamHasBytesAvailable", optional=true)
    public native boolean hasBytesAvailable();
    @Bridge(symbol="CFReadStreamRead", optional=true)
    private native @MachineSizedSInt long read(@Pointer long buffer, @MachineSizedSInt long bufferLength);
    @Bridge(symbol="CFReadStreamGetBuffer", optional=true)
    private native BytePtr getBuffer(@MachineSizedSInt long maxBytesToRead, MachineSizedSIntPtr numBytesRead);
    @Bridge(symbol="CFReadStreamCopyProperty", optional=true)
    private native @com.bugvm.rt.bro.annotation.Marshaler(CFType.NoRetainMarshaler.class) CFType getProperty(CFString propertyName);
    @Bridge(symbol="CFReadStreamSetProperty", optional=true)
    private native boolean setProperty(CFString propertyName, CFType propertyValue);
    @Bridge(symbol="CFReadStreamSetClient", optional=true)
    private native boolean setClient(CFStreamEventType streamEvents, FunctionPtr clientCB, CFStreamClientContext clientContext);
    @Bridge(symbol="CFReadStreamScheduleWithRunLoop", optional=true)
    public native void scheduleInRunLoop(CFRunLoop runLoop, String runLoopMode);
    @Bridge(symbol="CFReadStreamUnscheduleFromRunLoop", optional=true)
    public native void unscheduleFromRunLoop(CFRunLoop runLoop, String runLoopMode);
    /**
     * @since Available in iOS 7.0 and later.
     */
    @WeaklyLinked
    @Bridge(symbol="CFReadStreamSetDispatchQueue", optional=true)
    public native void setDispatchQueue(DispatchQueue q);
    /**
     * @since Available in iOS 7.0 and later.
     */
    @WeaklyLinked
    @Bridge(symbol="CFReadStreamCopyDispatchQueue", optional=true)
    public native DispatchQueue getDispatchQueue();
    @Bridge(symbol="CFReadStreamGetError", optional=true)
    public native @ByVal CFStreamError getStreamError();
    /**/
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy