⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pi3servletinputstream.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission. 

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/Pi3ServletInputStream.cpp,v $
 * $Date: 2003/05/13 18:42:19 $
 *
 Description:
	Execute an servlet extension.

\*____________________________________________________________________________*/
//$SourceTop:$

#include <jni.h>                                        /* where everything is defined */
#include "org_pi3_servlet_core_Pi3ServletInputStream.h" /* automatically created JNI header */
#include "PiAPI.h"
#define PI3_INTERNAL
#include "Pi3API.h"
#include "ServletHandler.h"
#include <wchar.h>

/*
 * Class:     org_pi3_servlet_core_Pi3ServletInputStream
 * Method:    available
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_org_pi3_servlet_core_Pi3ServletInputStream_available
(JNIEnv *env, jobject ins) {
	jclass cls = env->GetObjectClass(ins);
	jmethodID getPiHttp;
	getPiHttp = env->GetMethodID( cls, "getPiHttp", "()I");
	// Errorhandling!
	if (!getPiHttp) { return -1; };
	env->ExceptionClear();

	int PiHttp = env->CallIntMethod(ins, getPiHttp);
	if ( env->ExceptionOccurred() )
		{
		env->ExceptionDescribe();
		env->ExceptionClear();
		// Errorhandling!
		return -1;
		};

	/* ---
	check PIIOBuffer if data available
	--- */
	PIHTTP *pPIHTTP = (PIHTTP *)PiHttp;
	int iRes = PIIOBuffer_pollBeforeRead( pPIHTTP->pBuffer );
	PIIOBuffer *pBuf = (PIIOBuffer *)(pPIHTTP->pBuffer);
	return (iRes <= 0) ? 0 : pBuf->getBufferLen();
#undef PI3_INTERNAL
};

/*
 * Class:     org_pi3_servlet_core_Pi3ServletInputStream
 * Method:    internalRead
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_org_pi3_servlet_core_Pi3ServletInputStream_internalReadB
(JNIEnv *env, jobject ins) {

	jclass cls = env->GetObjectClass(ins);
	jmethodID getPiHttp;
	getPiHttp = env->GetMethodID( cls, "getPiHttp", "()I");
	// Errorhandling!
	if (!getPiHttp) { return -1; };
	env->ExceptionClear();

	int PiHttp = env->CallIntMethod(ins, getPiHttp);
	if ( env->ExceptionOccurred() )
		{
		env->ExceptionDescribe();
		env->ExceptionClear();
		// Errorhandling!
		return -1;
		};

	/* ---
	read 1 byte from PIIOBuffer
	--- */
	PIHTTP *pPIHTTP = (PIHTTP *)PiHttp;
	char cBuf;
	int iRes = PIIOBuffer_pollBeforeRead( pPIHTTP->pBuffer );
    if (iRes <= 0) { goto read_error; };

	iRes = PIIOBuffer_readToBuffer( pPIHTTP->pBuffer, &cBuf, 1 );

	/* ---
	copy char to wide char
	--- */
	if (iRes > 0) {
		wchar_t wc;
		return mbtowc(&wc, &cBuf, 1) 
			> 0 ? wc : -1;
	};

read_error:
	switch (iRes) {
	    case  0: return -1; // stream closed
        case -1: {
					cls = env->FindClass("java/io/IOException");
					if (cls) { env->ThrowNew(cls, "Error in PIIOBuffer_readToBuffer."); };
					// fall through default (return piRead)
				 };
        default:  return iRes;
	};
};

/*
 * Class:     org_pi3_servlet_core_Pi3ServletInputStream
 * Method:    internalReadA
 * Signature: ([BII)I
 */
JNIEXPORT jint JNICALL Java_org_pi3_servlet_core_Pi3ServletInputStream_internalReadA
(JNIEnv *env, jobject ins, jbyteArray arr, jint off, jint len) {

	jclass cls = env->GetObjectClass(ins);
	jmethodID getPiHttp;
	getPiHttp = env->GetMethodID( cls, "getPiHttp", "()I");
	// Errorhandling!
	if (!getPiHttp) { return -1; };
	env->ExceptionClear();

	int PiHttp = env->CallIntMethod(ins, getPiHttp);
	if ( env->ExceptionOccurred() )
		{
		env->ExceptionDescribe();
		env->ExceptionClear();
		// Errorhandling!
		return -1;
		};

	/* ---
	read len bytes from PIIOBuffer
	--- */
	PIHTTP *pPIHTTP = (PIHTTP *)PiHttp;
	char *szBuf;
	int iRes = PIIOBuffer_pollBeforeRead( pPIHTTP->pBuffer );
    if (iRes <= 0) { goto read_error; };

	szBuf = (char *)PIUtil_malloc(len);
	iRes = PIIOBuffer_readToBuffer( pPIHTTP->pBuffer, szBuf, len );

	/* ---
	copy char buffer to java array
	--- */
	if (iRes > 0) {
		// off + len <= array size is already checked on the java side!
		env->SetByteArrayRegion(arr, off, iRes, (jbyte *)szBuf); 
	};

	PIUtil_free(szBuf);

	if ( env->ExceptionOccurred() )
		{
		env->ExceptionDescribe();
		env->ExceptionClear();
		// Errorhandling!
		return -1;
		};

read_error:
	switch (iRes) {
	    case  0: return -1; // stream closed
        case -1: {
					cls = env->FindClass("java/io/IOException");
					if (cls) { env->ThrowNew(cls, "Error in PIIOBuffer_readToBuffer."); };
					// fall through default (return piRead)
				 };
        default:  return iRes;
	};
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -