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

📄 fileconnect.c

📁 java 1.1 gemini 08_16
💻 C
📖 第 1 页 / 共 3 页
字号:
{
	int rv=-1;


	if (FS_Seek(handle, size, FS_FILE_BEGIN) >= 0) {
		if (FS_Truncate(handle)==FS_NO_ERROR)
			rv = handle;
	}
	
	if (rv == -1) {
		*ppszError = "file truncated error.";
	} else {
		*ppszError = NULL;
	}

	return rv;

}

int fileSize(char** ppszError, int handle){
    int returnSize = 0;
    if(handle < 0){
		*ppszError = "get file size error.";
    }else{
        FS_GetFileSize(handle, (unsigned int*)&returnSize);
    }
    return returnSize;
}

int fileGetAttr(char** ppszError, kal_wchar* pszUCS2Filename){
    int attr;
    int returnValue = 0;
    attr = FS_GetAttributes(pszUCS2Filename);
    if(attr < 0){
        *ppszError = "get file attribute error.";
        return -1;
    }
    
    if(attr & FS_ATTR_HIDDEN)
        returnValue |= HIDDENMASK;
    if(attr & FS_ATTR_DIR)
        returnValue |= DIRECTORYMASK;
    if((attr & FS_ATTR_READ_ONLY) == 0)
        returnValue |= WRITABLEMASK;

    return returnValue;
}

KNIEXPORT KNI_RETURNTYPE_BOOLEAN
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_checkPlayRightAndConsume(void){
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif     
    KNI_ReturnBoolean(KAL_FALSE);
}
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_open(void){
    int   ioMode = KNI_GetParameterAsInt(2);
    kal_bool   is_DRM = KNI_GetParameterAsInt(3);
    kal_wchar* pszUCS2Filename;
    int   handle = -1;
    char* pszError = NULL;
    int   nameLen;

    KNI_StartHandles(2);
    KNI_DeclareHandle(thisObject);
    KNI_DeclareHandle(filename);

    KNI_GetThisPointer(thisObject);
    KNI_GetParameterAsObject(1, filename);

    nameLen = KNI_GetArrayLength(filename) * 2;
    pszUCS2Filename = (kal_wchar*)midpMalloc(nameLen+2);

    if (pszUCS2Filename == NULL) {
        KNI_ThrowNew("java/lang/OutOfMemoryError", "file connection Open");
    } else {
        
        KNI_GetRawArrayRegion(filename, 0, nameLen, (jbyte*)pszUCS2Filename);
        pszUCS2Filename[nameLen >> 1] = '\0';
        handle = fileOpen(&pszError, pszUCS2Filename, ioMode, is_DRM);
        midpFree(pszUCS2Filename);

        if (pszError != NULL) {
            KNI_ThrowNew("java/io/IOException", pszError);
            storageFreeError(pszError);
        } else {
	        KNI_registerCleanup(thisObject, fileCleanup);
	    }
    }

    KNI_EndHandles();
    KNI_ReturnInt(handle);
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_close(void){
    int handle = KNI_GetParameterAsInt(1);
    BOOL is_DRM = KNI_GetParameterAsInt(2);
    char* pszError = NULL;

    fileClose(&pszError, handle, is_DRM);
    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }
    KNI_ReturnVoid();
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_position(void){
    long  absolutePosition = (long)KNI_GetParameterAsInt(2);
    int   handle           = KNI_GetParameterAsInt(1);
    char* pszError = NULL;

    filePosition(&pszError, handle, absolutePosition);
    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }
    KNI_ReturnVoid();
}

KNIEXPORT KNI_RETURNTYPE_INT
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_read(void){
    kal_bool is_DRM    = KNI_GetParameterAsInt(5);
    int   length = KNI_GetParameterAsInt(4);
    int   offset = KNI_GetParameterAsInt(3);
    int   handle = KNI_GetParameterAsInt(1);
    int   bytesRead;

    char* pszError = NULL;

    KNI_StartHandles(1);
    KNI_DeclareHandle(buffer);

    KNI_GetParameterAsObject(2, buffer);

    bytesRead = fileRead(&pszError, handle, (char *)&(((BYTEARRAY)(*buffer))->bdata[offset]), length, is_DRM);

    KNI_EndHandles();

    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }
    KNI_ReturnInt(bytesRead);
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_write(void){
    int   length = KNI_GetParameterAsInt(4);
    int   offset = KNI_GetParameterAsInt(3);
    int   handle = KNI_GetParameterAsInt(1);
    char* pszError = NULL;

    KNI_StartHandles(1);
    KNI_DeclareHandle(buffer);

    KNI_GetParameterAsObject(2, buffer);

    fileWrite(&pszError, handle, (char *)&(((BYTEARRAY)(*buffer))->bdata[offset]), length);

    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}

KNIEXPORT KNI_RETURNTYPE_INT
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_truncateStream(void){
    int   size   = KNI_GetParameterAsInt(2);
    int   handle = KNI_GetParameterAsInt(1);
    char* pszError = NULL;

    handle = fileTruncate(&pszError, handle, size);

    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }
    KNI_ReturnInt(handle);
}

KNIEXPORT KNI_RETURNTYPE_INT
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_getFileSize(void){
    int   handle = KNI_GetParameterAsInt(1);
    int   size;
    char* pszError = NULL;
    size = fileSize(&pszError, handle);

    if (pszError != NULL) {
        KNI_ThrowNew("java/io/IOException", pszError);
        storageFreeError(pszError);
    }

    KNI_ReturnInt(size);
}

KNIEXPORT KNI_RETURNTYPE_INT
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_getAttribute(void){
    int   attr = 0;
    kal_wchar* pszUCS2Filename;
    char* pszError = NULL;
    int   nameLen;

    KNI_StartHandles(1);
    KNI_DeclareHandle(filename);

    KNI_GetParameterAsObject(1, filename);

    nameLen = KNI_GetArrayLength(filename) * 2;

    pszUCS2Filename = (kal_wchar*)midpMalloc(nameLen+2);
    if (pszUCS2Filename == NULL) {
        KNI_ThrowNew("java/lang/OutOfMemoryError", "file getAttribute");
    } else {
        
        KNI_GetRawArrayRegion(filename, 0, nameLen, (jbyte*)pszUCS2Filename);
        pszUCS2Filename[nameLen >> 1] = '\0';
        if(pszUCS2Filename[(nameLen >> 1) - 1] == '\\')
            pszUCS2Filename[(nameLen >> 1) - 1] = 0;
        attr = fileGetAttr(&pszError, pszUCS2Filename);

        midpFree(pszUCS2Filename);

        if (pszError != NULL) {
            attr = -1;
            storageFreeError(pszError);
        }
    }

    KNI_EndHandles();
    KNI_ReturnInt(attr);
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_setHidden(void){
    jboolean hidden = KNI_GetParameterAsBoolean(3);
    kal_wchar* pszUCS2Filename;
    int   nameLen;

    KNI_StartHandles(1);
    KNI_DeclareHandle(filename);

    KNI_GetParameterAsObject(2, filename);

    nameLen = KNI_GetArrayLength(filename) * 2;
    
    pszUCS2Filename = (kal_wchar*)midpMalloc(nameLen+2);
    if (pszUCS2Filename == NULL) {
        KNI_ThrowNew("java/lang/OutOfMemoryError", "set Hidden");
    } else {
        int attr;
        KNI_GetRawArrayRegion(filename, 0, nameLen, (jbyte*)pszUCS2Filename);
        pszUCS2Filename[nameLen >> 1] = '\0';
        attr = FS_GetAttributes(pszUCS2Filename);
        if(attr < 0){
            KNI_ThrowNew("java/io/IOException", "Get attribute error in set hidden");
        }else{
            if(hidden)
                attr |= FS_ATTR_HIDDEN;
            else
                attr &= ~FS_ATTR_HIDDEN;
            if(FS_SetAttributes(pszUCS2Filename, (BYTE)attr) != FS_NO_ERROR)
                KNI_ThrowNew("java/io/IOException", "Set attribute error in set hidden");
        }
        midpFree(pszUCS2Filename);
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_setWritable(void){
    jboolean writable = KNI_GetParameterAsBoolean(3);
    kal_wchar* pszUCS2Filename;
    int   nameLen;

    KNI_StartHandles(1);
    KNI_DeclareHandle(filename);

    KNI_GetParameterAsObject(2, filename);

    nameLen = KNI_GetArrayLength(filename) * 2;
    
    pszUCS2Filename = (kal_wchar*)midpMalloc(nameLen+2);
    if (pszUCS2Filename == NULL) {
        KNI_ThrowNew("java/lang/OutOfMemoryError", "set writable");
    } else {
        int attr;
        KNI_GetRawArrayRegion(filename, 0, nameLen, (jbyte*)pszUCS2Filename);
        pszUCS2Filename[nameLen >> 1] = '\0';
        attr = FS_GetAttributes(pszUCS2Filename);
        if(attr < 0){
            KNI_ThrowNew("java/io/IOException", "Get attribute error in set writtable");
        }else{
            if(writable)
                attr &= ~FS_ATTR_READ_ONLY;
            else
                attr |= FS_ATTR_READ_ONLY;
            if(FS_SetAttributes(pszUCS2Filename, (BYTE)attr) != FS_NO_ERROR)
                KNI_ThrowNew("java/io/IOException", "Set attribute error in set writtable");
        }
        midpFree(pszUCS2Filename);
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}

kal_uint64 jvm_get_julian_time_ms(unsigned short year, unsigned char month, unsigned char day, 
									unsigned char hour, unsigned char minute, unsigned char second, int milli);

extern float mmi_dt_get_tz(void);

KNIEXPORT KNI_RETURNTYPE_LONG
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_getLastModified(void){
    int   handle = KNI_GetParameterAsInt(1);
    ulong64 returnTime = 0;
    FS_FileInfo fileInfo;
    if(FS_GetFileInfo(handle, &fileInfo) != FS_NO_ERROR){
        KNI_ThrowNew("java/io/IOException", "get file info error.");
    }else{
        FS_DOSDateTime tempTime = fileInfo.DirEntry->DateTime;
        returnTime = jvm_get_julian_time_ms((unsigned short)(tempTime.Year1980 + 1980), (unsigned char)(tempTime.Month - 1), 
											(unsigned char)tempTime.Day, (unsigned char)tempTime.Hour, (unsigned char)tempTime.Minute, 
											(unsigned char)(tempTime.Second2 * 2), 0);
        returnTime -= (ulong64)(((int)(mmi_dt_get_tz() * 60)) * 60 * 1000);
    }

    KNI_ReturnLong(returnTime);
}

KNIEXPORT KNI_RETURNTYPE_LONG
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_getTotalSize(void){
    unsigned short drive = KNI_GetParameterAsChar(1);
    ulong64 free_space = 0;
	FS_DiskInfo DiskInfo;
    kal_wchar path[4] = {'C', ':', '\\', 0};
    path[0] = drive;
	if(FS_GetDiskInfo(path, &DiskInfo, FS_DI_FREE_SPACE|FS_DI_BASIC_INFO) >= 0)
	    free_space = (ulong64)DiskInfo.TotalClusters * (ulong64)DiskInfo.SectorsPerCluster * (ulong64)DiskInfo.BytesPerSector;
    else
        KNI_ThrowNew("java/io/IOException", "Get disk info failed in getTotalSize.");
    KNI_ReturnLong(free_space);
}

KNIEXPORT KNI_RETURNTYPE_LONG
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_getUsedSize(void){
    unsigned short drive = KNI_GetParameterAsChar(1);
    ulong64 free_space = 0;
	FS_DiskInfo DiskInfo;
    kal_wchar path[4] = {'C', ':', '\\', 0};
    path[0] = drive;
	if(FS_GetDiskInfo(path, &DiskInfo, FS_DI_FREE_SPACE|FS_DI_BASIC_INFO) >= 0)
	    free_space = (ulong64)(DiskInfo.TotalClusters - DiskInfo.FreeClusters) * 
                     (ulong64)DiskInfo.SectorsPerCluster * (ulong64)DiskInfo.BytesPerSector;
    else
        KNI_ThrowNew("java/io/IOException", "Get disk info failed in getUsedSize.");
    KNI_ReturnLong(free_space);
}

KNIEXPORT KNI_RETURNTYPE_LONG
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_diskAvailableSize(void){
    unsigned short drive = KNI_GetParameterAsChar(1);
    ulong64 free_space = 0;
	FS_DiskInfo DiskInfo;
    kal_wchar path[4] = {'C', ':', '\\', 0};
    path[0] = drive;
	if(FS_GetDiskInfo(path, &DiskInfo, FS_DI_FREE_SPACE|FS_DI_BASIC_INFO) >= 0)
	    free_space = (ulong64)DiskInfo.FreeClusters * (ulong64)DiskInfo.SectorsPerCluster * (ulong64)DiskInfo.BytesPerSector;
    else
        KNI_ThrowNew("java/io/IOException", "Get disk info failed in diskAvailableSize.");
    KNI_ReturnLong(free_space);
}

KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_renameImpl(void){
    kal_wchar* pszOldUCS2Filename;
    kal_wchar* pszNewUCS2Filename;
    int   nameLen;
    
    KNI_StartHandles(2);
    KNI_DeclareHandle(oldfilename);
    KNI_DeclareHandle(newfilename);

    KNI_GetParameterAsObject(1, oldfilename);
    KNI_GetParameterAsObject(2, newfilename);

⌨️ 快捷键说明

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