private void startSession() { SipClientConnection scc = null; try { // <i><b>start a listener for incoming requests</b></i> startListener(); // <i><b>open SIP connection with remote user</b></i> scc = (SipClientConnection) Connector.open(address.getString()); scc.setListener(this); // <i><b>initialize INVITE request</b></i> scc.initRequest("INVITE", scn); scc.setHeader("Content-Length", "" + sdp.length()); scc.setHeader("Content-Type", "application/sdp"); OutputStream os = scc.openContentOutputStream(); os.write(sdp.getBytes()); os.close(); // <i><b>close and send</b></i> str = new StringItem("Inviting... ", scc.getHeader("To")); form.append(str); } catch (Exception ex) { ex.printStackTrace(); // <i><b>handle IOException</b></i> } }
/** Safely closes connection and streams */ public static void close(Connection conn, InputStream is, OutputStream os) { if (os != null) { try { os.close(); } catch (IOException e) { } } if (is != null) { try { is.close(); } catch (IOException e) { } } if (conn != null) { try { conn.close(); } catch (IOException e) { } } }
private void pump(InputStream in, OutputStream out, int size) throws IOException { byte[] buffer = new byte[size]; int count; while ((count = in.read(buffer, 0, size)) >= 0) out.write(buffer, 0, count); out.flush(); }
public InputStream makeRequest(String url, OutputStream pos) throws IOException { HttpConnection conn = null; ByteArrayOutputStream bos = null; DataInputStream is = null; DataOutputStream os = null; InputStream pis = null; try { conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestProperty("Accept", "application/octet-stream"); if (sessionCookie == null) { conn.setRequestProperty("version", "???"); } else { conn.setRequestProperty("cookie", sessionCookie); } // Getting the output stream may flush the headers os = conn.openDataOutputStream(); os.write(pos.getBytes()); os.close(); int responseCode; try { responseCode = conn.getResponseCode(); } catch (IOException e) { throw new IOException("No response from " + url); } if (responseCode != HttpConnection.HTTP_OK) { throw new IllegalArgumentException(); } bos = new ByteArrayOutputStream(); { is = conn.openDataInputStream(); String sc = conn.getHeaderField("set-cookie"); if (sc != null) { sessionCookie = sc; } while (true) { int ch = is.read(); if (ch == -1) break; bos.write(ch); } is.close(); is = null; conn.close(); conn = null; } pis = new InputStream(bos.toByteArray()); bos.close(); bos = null; } catch (Exception e) { e.printStackTrace(); try { if (conn != null) { conn.close(); conn = null; } if (bos != null) { bos.close(); bos = null; } if (is != null) { is.close(); is = null; } } catch (Exception exc) { } throw new IOException(); } return pis; }
public void run_0420() { FileConnection conn1 = null; int counter = 0; try { // Make a connection to the new file. conn1 = (FileConnection) Connector.open( "file:///" + iRoot + "mainTestDir/test040/new042File.txt", Connector.READ_WRITE); counter = 1; System.out.println(" run_0420: counter = 1 "); if (conn1.exists()) throw new TestFailedException(); counter = 2; System.out.println(" run_0420: counter = 2 "); conn1.create(); counter = 3; System.out.println(" run_0420: counter = 3 "); OutputStream stream = conn1.openOutputStream(); counter = 4; System.out.println(" run_0420: counter = 4 "); byte[] byteArray1 = new byte[50]; for (int i = 0; i < byteArray1.length; ++i) byteArray1[i] = 55; counter = 5; System.out.println(" run_0420: counter = 5 "); stream.write(byteArray1); stream.close(); counter = 6; System.out.println(" run_0420: counter = 6 "); // Re-open an OutputStream. stream = conn1.openOutputStream(conn1.fileSize()); counter = 7; System.out.println(" run_0420: counter = 7 "); for (int i = 0; i < byteArray1.length; ++i) byteArray1[i] = 56; conn1.close(); counter = 8; stream.write(byteArray1); counter = 9; stream.close(); System.out.println(" Going to second part of run_0420"); // Re-open a connection. conn1 = (FileConnection) Connector.open( "file:///" + iRoot + "mainTestDir/test040/new042File.txt", Connector.READ_WRITE); counter = 10; System.out.println(" run_0420: counter = 10 "); if (!conn1.exists()) throw new TestFailedException(); counter = 11; System.out.println(" run_0420: counter = 11 "); InputStream inStream = conn1.openInputStream(); counter = 12; System.out.println(" run_0420: counter = 12 "); byte[] byteArray2 = new byte[110]; int retValue = inStream.read(byteArray2); if (retValue != 100) throw new TestFailedException("retValue: " + retValue); counter = 13; System.out.println(" run_0420: counter = 13 "); for (int i = 0; i < retValue; ++i) { if (i < 50) { if (byteArray2[i] != 55) throw new TestFailedException("i: " + i + " byteArray2[i]: " + byteArray2[i]); } else if (i > 49) { if (byteArray2[i] != 56) throw new TestFailedException("i: " + i + " byteArray2[i]: " + byteArray2[i]); } } // end for counter = 14; conn1.close(); inStream.close(); assertTrue(" run_0420() ok ", true); } catch (Exception ex) { append(ex.toString()); append("TEST FAILED!!! Case: run_0420() counter: " + counter + "\n"); assertTrue(" run_0420() FAILED!!! Counter: " + counter, false); } } // end run_0420()