Java Code Examples for javax.microedition.io.StreamConnection

The following examples show how to use javax.microedition.io.StreamConnection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: luaj   Source File: JmeIoLib.java    License: MIT License 6 votes vote down vote up
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
	String url = "file:///" + filename;
	int mode  = readMode? Connector.READ: Connector.READ_WRITE;
	StreamConnection conn = (StreamConnection) Connector.open( url, mode );
	File f = readMode? 
			new FileImpl(conn, conn.openInputStream(), null):
			new FileImpl(conn, conn.openInputStream(), conn.openOutputStream());
	/*
	if ( appendMode ) {
		f.seek("end",0);
	} else {
		if ( ! readMode )
			conn.truncate(0);
	}
	*/
	return f;
}
 
Example 2
public static void onMessage(final PushInputStream stream, final StreamConnection sc) {
    if(pushCallback != null) {
        new Thread() {
            public void run() {
                try {
                    final byte[] buffer = Util.readInputStream(stream);
                    try {
                        stream.accept();
                        Util.cleanup(stream);
                        sc.close();
                    } catch(Throwable t) {}
                    updateIndicator(unreadCount + 1);
                    Display.getInstance().callSerially(new Runnable() {
                        public void run() {
                            pushCallback.push(new String(buffer));
                        }
                    });
                } catch(IOException err) {
                    err.printStackTrace();
                }
            }
        }.start();
    }
}
 
Example 3
Source Project: pluotsorbet   Source File: Protocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Disconnect from the underlying socket transport.
 * Closes the low level socket connection and the input and 
 * output streams used by the socket.
 * <p>
 * Warning: A subclass that implements connect, should also implement this
 * method without calling this method.
 *
 * @param connection connection return from {@link #connect()}
 * @exception IOException if an I/O error occurs while
 *                  the connection is terminated.
 * @exception IOException is thrown if the connection or 
 *                        associated streams cannot be closed
 */
protected void disconnect(StreamConnection connection)
       throws IOException {
    try {
        if (connection != null) {
            connection.close();
        }
    } finally {
        try {
            if (streamOutput != null) {
                streamOutput.close();
                streamOutput = null;
            }
        } finally {
            if (streamInput != null) {
                streamInput.close();
                streamInput = null;
            }
        }
    }
}
 
Example 4
void testBasicSSLStreamConnection() throws IOException {
    StreamConnection t = (StreamConnection)Connector.open(SOCKET_URL);
    try {
        SSLStreamConnection s =
            new SSLStreamConnection(HOST, PORT, t.openInputStream(), t.openOutputStream(), KEY_STORE);
        OutputStream os = s.openOutputStream();
        InputStream is = s.openInputStream();

        send(os, MESSAGE);
        th.check(receive(is), MESSAGE);

        os.close();
        is.close();
        s.close();
    } finally {
        t.close();
    }
}
 
Example 5
void testMultipleSendsReceivesOnSameSocket() throws IOException {
    StreamConnection t = (StreamConnection)Connector.open(SOCKET_URL);
    try {
        SSLStreamConnection s =
            new SSLStreamConnection(HOST, PORT, t.openInputStream(), t.openOutputStream(), KEY_STORE);
        OutputStream os = s.openOutputStream();
        InputStream is = s.openInputStream();

        for (int i = 0; i < 100; i++) {
            String message = "Message n." + i;
            send(os, message);
            th.check(receive(is), message);
        }

        os.close();
        is.close();
        s.close();
    } finally {
        t.close();
    }
}
 
Example 6
void testMultipleSendsReceivesOnMultipleSockets() throws IOException {
    for (int i = 0; i < 100; i++) {
        StreamConnection t = (StreamConnection)Connector.open(SOCKET_URL);
        try {
            SSLStreamConnection s =
                new SSLStreamConnection(HOST, PORT, t.openInputStream(), t.openOutputStream(), KEY_STORE);
            OutputStream os = s.openOutputStream();
            InputStream is = s.openInputStream();

            String message = "Message n." + i;
            send(os, message);
            th.check(receive(is), message);

            os.close();
            is.close();
            s.close();
        } finally {
            t.close();
        }
    }
}
 
Example 7
void testSendOnClosedOutputStream() throws IOException {
    StreamConnection t = (StreamConnection)Connector.open(SOCKET_URL);
    try {
        SSLStreamConnection s =
            new SSLStreamConnection(HOST, PORT, t.openInputStream(), t.openOutputStream(), KEY_STORE);
        OutputStream os = s.openOutputStream();
        InputStream is = s.openInputStream();

        os.close();
        try {
            send(os, MESSAGE);
            th.fail("send on closed output stream");
        } catch(Exception e) {
            th.check(e, "java.io.InterruptedIOException: Stream closed");
        }

        is.close();
        s.close();
    } finally {
        t.close();
    }
}
 
Example 8
void testReceiveOnClosedInputStream() throws IOException {
    StreamConnection t = (StreamConnection)Connector.open(SOCKET_URL);
    try {
        SSLStreamConnection s =
            new SSLStreamConnection(HOST, PORT, t.openInputStream(), t.openOutputStream(), KEY_STORE);
        OutputStream os = s.openOutputStream();
        InputStream is = s.openInputStream();

        send(os, MESSAGE);
        is.close();
        try {
            receive(is);
            th.fail("receive on closed input stream");
        } catch(Exception e) {
            th.check(e, "java.io.InterruptedIOException: Stream closed");
        }

        os.close();
        s.close();
    } finally {
        t.close();
    }
}
 
Example 9
Source Project: J2ME-Loader   Source File: Connection.java    License: Apache License 2.0 5 votes vote down vote up
public StreamConnection acceptAndOpen() throws IOException {
	if (serverSocket == null) {
		throw new IOException();
	}
	socket = serverSocket.accept();
	return new SPPConnectionImpl(socket, skipAfterWrite);
}
 
Example 10
/**
 * Create a new instance of this class.
 *
 * @param p_protocol protocol for the connection
 * @param p_host     hostname for the connection
 * @param p_port     port number for the connection
 * @param p_sc       stream connection
 * @param p_dos      data output stream from the stream connection
 * @param p_dis      data input stream from the stream connection
 */
StreamConnectionElement(String p_protocol,
                        String p_host,
                        int p_port,
                        StreamConnection p_sc,
                        DataOutputStream p_dos,
                        DataInputStream p_dis) {
    m_protocol = p_protocol;
    m_host = p_host;
    m_port = p_port;
    m_stream = p_sc;
    m_data_output_stream = p_dos;
    m_data_input_stream = p_dis;
    m_time = System.currentTimeMillis();
}
 
Example 11
Source Project: J2ME-Loader   Source File: ServerSocketConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public StreamConnection acceptAndOpen() throws IOException {
	return new SocketConnection(serverSocket.accept());
}
 
Example 12
public StreamConnection acceptAndOpen() throws IOException {
return new SocketConnectionImpl(factory, socket.accept());
 }
 
Example 13
Source Project: luaj   Source File: JmeIoLib.java    License: MIT License 4 votes vote down vote up
private FileImpl( StreamConnection conn, InputStream is, OutputStream os ) {
	this.conn = conn;
	this.is = is;
	this.os = os;
}
 
Example 14
Source Project: pluotsorbet   Source File: BTGOEPNotifier.java    License: GNU General Public License v2.0 4 votes vote down vote up
public ObexTransport acceptAndOpen() throws IOException {
    return createTransportConnection(
            (StreamConnection)(notifier.acceptAndOpen()));
}
 
Example 15
Source Project: pluotsorbet   Source File: BTGOEPNotifier.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected BTGOEPConnection createTransportConnection(
        StreamConnection sock) throws IOException {
    return new BTGOEPConnection(sock);
}
 
Example 16
Source Project: pluotsorbet   Source File: BTGOEPConnection.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected BTGOEPConnection(StreamConnection sock) throws IOException {
    this.sock = sock;
    is = sock.openInputStream();
    os = sock.openOutputStream();
}
 
Example 17
/**
 * Tries to add a reuseable connection to the connection pool.
 * Replace any not in use connections to the same host and port.
 * Will not add to the same host and port in use. Will replace
 * oldest not in use element (if any) if the pool is full.
 * 
 * @param p_protocol            The protocol for the connection
 * @param p_host                The Hostname for the connection
 * @param p_port                The port number for the connection
 * @param sc                    The base stream connection
 * @param dos                   The data output stream from the base
 *                                connection
 * @param dis                   The data input stream from the base
 *                                connection
 *
 * @return true if the connection was added, otherwise false
 */
synchronized boolean add(String p_protocol,
        String p_host, int p_port, StreamConnection sc,
        DataOutputStream dos, DataInputStream dis) {

    StreamConnectionElement oldestNotInUse = null;

    // find the last unused element
    Enumeration cons = m_connections.elements();
    while (cons.hasMoreElements()) {
        StreamConnectionElement sce =
            (StreamConnectionElement)cons.nextElement();

        if (sce.m_in_use) {
            if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
                return false;
            }

            continue;
        }

        // if the connection is a duplicate, delete it
        if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
            // no protocol duplicates on host and port
            sce.close();
            m_connections.removeElement(sce);
            break;
        } else {
            if (oldestNotInUse == null ||
                sce.m_time < oldestNotInUse.m_time) {
                // save the oldest not in use, it may be removed later
                oldestNotInUse = sce;
            }
        }
    }

    /*
     * first check and see if the maximum number of connections
     * has been reached - if so delete the first one in the list (FIFO)
     *   or
     * if this port and host are already in the pool.
     */
    if (m_connections.size() >= m_max_connections) {
        if (oldestNotInUse == null) {
            return false;
        }

        oldestNotInUse.close();
        m_connections.removeElement(oldestNotInUse);
    }
 
    m_connections.addElement(new StreamConnectionElement(p_protocol,
                      p_host, p_port, sc, dos, dis));
    return true;
}
 
Example 18
Source Project: pluotsorbet   Source File: TestHttpHeaders.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected StreamConnection connect() throws IOException {
    stream = new StubStreamConnection(inbuf);
    return stream;
}
 
Example 19
Source Project: pluotsorbet   Source File: Protocol.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the underlying stream connection.
 *
 * @return underlying stream connection
 */
protected StreamConnection getStreamConnection() {
    return streamConnection;
}
 
Example 20
/**
 * Get the stream connection for this element.
 *
 * @return                     base stream connection
 */
public StreamConnection getBaseConnection() {
    return m_stream;
}