public void OpenFile(int iocb, String filename, boolean readOnlyFlag) { if (iocb >= 0 && iocb < MAXFILES) { if (randomAccessFile[iocb] == null && httpConn[iocb] == null && fc[iocb] == null && socketConn[iocb] == null && inputStream[iocb] == null) { Class clazz; if (filename.startsWith("file:")) { try { fc[iocb] = (FileConnection) Connector.open(filename); if (readOnlyFlag) { if (!fc[iocb].exists()) { throw new BasicError(4130, "No file: " + filename); } } else { if (fc[iocb].exists()) { fc[iocb].delete(); fc[iocb].close(); fc[iocb] = (FileConnection) Connector.open(filename); } fc[iocb].create(); } dataInput[iocb] = fc[iocb].openDataInputStream(); if (readOnlyFlag) { dataOutput[iocb] = null; } else { dataOutput[iocb] = fc[iocb].openDataOutputStream(); } } catch (Exception ex) { clazz = ex.getClass(); throw new BasicError(4130, clazz.getName()); } } else if (filename.startsWith("socket:")) { try { socketConn[iocb] = (SocketConnection) Connector.open(filename); dataOutput[iocb] = socketConn[iocb].openDataOutputStream(); dataInput[iocb] = socketConn[iocb].openDataInputStream(); } catch (Exception ex) { clazz = ex.getClass(); throw new BasicError(4130, clazz.getName()); } } else if (filename.startsWith("https:")) { try { httpConn[iocb] = (HttpsConnection) Connector.open(filename, 3); if (readOnlyFlag) { httpConn[iocb].setRequestMethod("GET"); } else { httpConn[iocb].setRequestMethod("POST"); httpConn[iocb].setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } baos[iocb] = new ByteArrayOutputStream(); dataOutput[iocb] = new DataOutputStream(baos[iocb]); dataInput[iocb] = null; } catch (Exception ex) { clazz = ex.getClass(); throw new BasicError(4131, clazz.getName()); } } else if (filename.startsWith("http:")) { try { httpConn[iocb] = (HttpConnection) Connector.open(filename, 3); if (readOnlyFlag) { httpConn[iocb].setRequestMethod("GET"); } else { httpConn[iocb].setRequestMethod("POST"); httpConn[iocb].setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } //httpConn[iocb].setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLCD-1.1 (Mobile BASIC MIDlet 1.9.1 by kiriman & dzanis)"); //httpConn[iocb].setRequestProperty("Content-Language", "en-US"); //httpConn[iocb].setRequestProperty("Accept", "text/plain"); //httpConn[iocb].setRequestProperty("Content-Type", "text/plain"); // httpConn[iocb].setRequestProperty("Connection", "close"); baos[iocb] = new ByteArrayOutputStream(); dataOutput[iocb] = new DataOutputStream(baos[iocb]); dataInput[iocb] = null; } catch (Exception ex) { clazz = ex.getClass(); throw new BasicError(4131, clazz.getName()); } } else if (filename.startsWith("/")) { try { inputStream[iocb] = getClass().getResourceAsStream(filename); dataInput[iocb] = new DataInputStream(inputStream[iocb]); dataOutput[iocb] = null; } catch (Exception ex) { throw new BasicError(4133, ex.getClass().getName()); } } else { try { randomAccessFile[iocb] = new RandomAccessFile(filename, readOnlyFlag); dataInput[iocb] = randomAccessFile[iocb]; if (readOnlyFlag) { dataOutput[iocb] = null; } else { dataOutput[iocb] = randomAccessFile[iocb]; } } catch (Exception ex) { randomAccessFile[iocb] = null; dataInput[iocb] = null; dataOutput[iocb] = null; throw new BasicError(4132, ex.getClass().getName()); } } } else { throw new BasicError(4098, "Channel " + iocb + " already in use"); } } else { throw new BasicError(4096, "Invalid channel"); } } private void sub_lolclose(int iocb) { if (iocb >= 0 && iocb < MAXFILES && httpConn[iocb] != null && baos[iocb] != null) { try { httpConn[iocb].setRequestProperty("Content-Length", String.valueOf(baos[iocb].size())); OutputStream os = httpConn[iocb].openOutputStream(); byte[] b = baos[iocb].toByteArray(); os.write(b); os.close(); InputStream is = httpConn[iocb].openInputStream(); dataInput[iocb] = new DataInputStream(is); dataOutput[iocb] = null; } catch (IOException ex) { Class clazz = ex.getClass(); throw new BasicError(4133, clazz.getName()); } baos[iocb] = null; } } public void CloseFile(int iocb) { if (iocb >= 0 && iocb < MAXFILES) { if (httpConn[iocb] != null) { sub_lolclose(iocb); } try { if (randomAccessFile[iocb] != null) { randomAccessFile[iocb].close(); } if (fc[iocb] != null) { if (dataOutput[iocb] != null) { ((DataOutputStream) dataOutput[iocb]).close(); } ((DataInputStream) dataInput[iocb]).close(); fc[iocb].close(); } if (socketConn[iocb] != null) { ((DataOutputStream) dataOutput[iocb]).close(); ((DataInputStream) dataInput[iocb]).close(); socketConn[iocb].close(); } if (inputStream[iocb] != null) { ((DataInputStream) dataInput[iocb]).close(); } } catch (Exception ex) { inputStream[iocb] = null; httpConn[iocb] = null; fc[iocb] = null; socketConn[iocb] = null; randomAccessFile[iocb] = null; dataInput[iocb] = null; dataOutput[iocb] = null; throw new BasicError(4134, ex.getMessage()); } inputStream[iocb] = null; httpConn[iocb] = null; fc[iocb] = null; socketConn[iocb] = null; randomAccessFile[iocb] = null; dataInput[iocb] = null; dataOutput[iocb] = null; } else { throw new BasicError(4096, "Invalid channel"); } }