1
/*******************************************************************************
2
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* QNX Software Systems - initial API and implementation
10
*******************************************************************************/
13
* This is a JNI implementation of access to standard i/o streams
21
#include "SpawnerInputStream.h"
22
#include "SpawnerOutputStream.h"
28
void ThrowByName(JNIEnv *env, const char *name, const char *msg);
30
#define BUFF_SIZE (1024)
32
/* Inaccessible static: skipBuffer */
34
* Class: SpawnerInputStream
38
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_read0
39
(JNIEnv * env, jobject proc, jint fd, jbyteArray buf, jint len)
41
unsigned char tmpBuf[BUFF_SIZE];
43
// printf("Come to read0\n");
44
while(len > nBuffOffset)
46
int nReadLen = min(len - nBuffOffset, BUFF_SIZE);
48
nread = read(fd, tmpBuf, nReadLen);
50
(*env) -> SetByteArrayRegion(env, buf, nBuffOffset, nReadLen, tmpBuf);
57
// printf("Leave read with %i bytes read\n", nBuffOffset);
58
return nBuffOffset; // This is a real full readed length
63
* Class: SpawnerInputStream
67
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_close0
68
(JNIEnv * env, jobject proc, jint fd)
74
* Class: SpawnerOutputStream
78
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerOutputStream_write0
79
(JNIEnv * env, jobject proc, jint fd, jbyteArray buf, jint len)
81
unsigned char tmpBuf[BUFF_SIZE];
84
// printf("Come to write0, len = %i\n", len);
85
while(len > nBuffOffset)
87
int nWriteLen = min(len - nBuffOffset, BUFF_SIZE);
88
(*env) -> GetByteArrayRegion(env, buf, nBuffOffset, nWriteLen, tmpBuf);
89
// printf("Write %i bytes; last byte = %#x\n", nWriteLen, (int)tmpBuf[nWriteLen - 1]);
90
if(nWriteLen != write(fd, tmpBuf, nWriteLen))
92
// printf("Error: written %i bytes; errno = %i, fd = %i, len = %i ", nWriteLen, errno, fd, rc);
93
ThrowByName(env, "java/io/IOException", strerror(errno));
95
nBuffOffset += nWriteLen;
101
* Class: SpawnerOutputStream
105
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerOutputStream_close0
106
(JNIEnv * env, jobject proc, jint fd)