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

com.codename1.impl.javase.cef.InputStreamSchemeHandler Maven / Gradle / Ivy

// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

package com.codename1.impl.javase.cef;

import com.codename1.io.Log;
import com.codename1.io.Util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.cef.callback.CefCallback;
import org.cef.handler.CefResourceHandlerAdapter;
import org.cef.misc.IntRef;
import org.cef.misc.StringRef;
import org.cef.network.CefRequest;
import org.cef.network.CefResponse;

/**
 * The example for the second scheme with domain handling is a more
 * complex example and is taken from the parent project CEF. Please
 * see CEF: "cefclient/scheme_test.cpp" for futher details
 */
public class InputStreamSchemeHandler extends CefResourceHandlerAdapter {
    

    
    public static final String scheme = "cn1stream";
    public static final String domain = "cn1app";
    //public static final String domain = "tests";
    private StreamWrapper stream;
    
    
    private byte[] data_;
    private String mime_type_;
    private int offset_ = 0;
    private boolean closed;
    
    

    public InputStreamSchemeHandler() {
        super();

    }
    
    public static String getURL(String streamId) {
        return scheme+"://"+domain+"/streams/"+streamId;
    }
    private static final int LIMIT = 65536*2;
    private int written;
    boolean rangeRequest;
    long rangeStart;
    
    @Override
    public synchronized boolean processRequest(CefRequest request, CefCallback callback) {
        
        Map headerMap = new HashMap();
        request.getHeaderMap(headerMap);
        String range = (String)headerMap.get("Range");
        
        System.out.println("In processRequest "+request.getURL()+" " +headerMap+" handler="+this);
        String url = request.getURL();
        String streamId = null;
        if (url.indexOf("/") != -1) {
            streamId = url.substring(url.lastIndexOf("/")+1);
        }
        
        if (streamId != null) {
            stream = BrowserPanel.getStreamRegistry().getStream(streamId);
            
            if (range != null) {
                if (range.indexOf("=") != -1) {
                    rangeRequest = true;
                    range = range.substring(range.indexOf("=")+1);
                    String startStr = range.substring(0, range.indexOf("-"));
                    long start = Long.parseLong(startStr);
                    rangeStart = start;
                    if (stream.getOffset() < start) {
                        InputStream inputStream = stream.getStream();
                        int seekTo = (int)(start- stream.getOffset());
                        try {
                            for (int i=0; i 0) {
                    System.out.println("Abbout to attempt reading "+bytes_to_read);
                    int read = stream.getStream().read(data_out, 0, bytes_to_read > 0 ? Math.min(bytes_to_read, data_out.length) : data_out.length);
                    
                    System.out.println("Read "+read+" from stream");
                    if (read == -1) {
                        System.out.println("Reached the end of the stream");
                        has_data = false;
                        bytes_read.set(0);
                        stream.getStream().close();
                        closed = true;
                        BrowserPanel.getStreamRegistry().removeStream(stream);
                        stream = null;
                        return false;
                    } else {
                        written += read;
                        //System.out.println("Zero bytes were available");
                        long oldOffset = stream.getOffset();
                        oldOffset += read;
                        stream.setOffset(oldOffset);
                        bytes_read.set(read);
                        return true;
                    }

                } else {
                    System.out.println("No bytes available");
                    bytes_read.set(0);
                    
                    
                }
            } catch (IOException ex) {
                Log.e(ex);
            }
            System.out.println("Returning "+has_data);
            
            return has_data;
        } finally {
            System.out.println("Exiting readResponse");
        }
    }
    

    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy