javax.microedition.io.Connector Java Examples

The following examples show how to use javax.microedition.io.Connector. 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 File: CanRead.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canRead() on a readable file
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Setting file as readable");
			conn.setReadable(true);
			
			boolean canRead = conn.canRead();
			addOperationDesc("canRead() returned " + canRead);
			
			passed = canRead==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests canRead() on a readable file", passed);
}
 
Example #2
Source File: LastModified.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public void test0002() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			long modifyTime = conn.lastModified();
			addOperationDesc("lastModified() returned " + modifyTime);
			
			passed = modifyTime == 0;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests lastModified() on a file (modification date is NOT supported by filesystem)", passed);
}
 
Example #3
Source File: ForegroundEnableBackgroundServiceMIDlet.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public void startApp() {
    System.out.println("START - Background alarm started: " + startedBackgroundAlarm());

    receiveSMS();

    try {
        FileConnection file = (FileConnection)Connector.open("file:////startBackgroundService");
        if (!file.exists()) {
            file.create();
        }
        file.close();
    } catch (IOException e) {
        System.out.println("Unexpected exception: " + e);
        return;
    }

    System.out.println("DONE - Background alarm started: " + startedBackgroundAlarm());
}
 
Example #4
Source File: Media.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads the content from the specified HTTP URL and returns InputStream
 * where the contents are read.
 * 
 * @return InputStream
 * @throws IOException
 */
private InputStream urlToStream(String url) throws IOException {
    // Open connection to the http url...
    HttpConnection connection = (HttpConnection) Connector.open(url);
    DataInputStream dataIn = connection.openDataInputStream();
    byte[] buffer = new byte[1000];
    int read = -1;
    // Read the content from url.
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
    while ((read = dataIn.read(buffer)) >= 0) {
        byteout.write(buffer, 0, read);
    }
    dataIn.close();
    // Fill InputStream to return with content read from the URL.
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout.toByteArray());
    return byteIn;
}
 
Example #5
Source File: VideoCanvas.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads the content from the specified HTTP URL and returns InputStream
 * where the contents are read.
 * 
 * @return InputStream
 * @throws IOException
 */
private InputStream urlToStream(String url) throws IOException {
    // Open connection to the http url...
    HttpConnection connection = (HttpConnection) Connector.open(url);
    DataInputStream dataIn = connection.openDataInputStream();
    byte[] buffer = new byte[1000];
    int read = -1;
    // Read the content from url.
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
    while ((read = dataIn.read(buffer)) >= 0) {
        byteout.write(buffer, 0, read);
    }
    dataIn.close();
    connection.close();
    // Fill InputStream to return with content read from the URL.
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout.toByteArray());
    return byteIn;
}
 
Example #6
Source File: SMSMIDlet.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    try {
        MessageConnection conn = (MessageConnection)Connector.open("sms://:5000");
        TextMessage message = (TextMessage)conn.receive();

        if (!message.getPayloadText().equals("Prova SMS")) {
            System.out.println("FAIL - Wrong SMS text: " + message.getPayloadText());
        }

        if (!message.getAddress().equals("sms://unknown:5000")) {
            System.out.println("FAIL - Wrong SMS address: " + message.getAddress());
        }
    } catch (Exception e) {
        System.out.println("FAIL - Unexpected exception: " + e);
        e.printStackTrace();
    }
}
 
Example #7
Source File: TotalSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests totalSize() as the sum of usedSize() and availableSize()
 */
public void test0003() {
	boolean passed = false;
	try {	
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			long totalSize = conn.totalSize();
			addOperationDesc("totalSize()="+totalSize);
			long totalSize2 = conn.availableSize() + conn.usedSize();
			addOperationDesc("availableSize()+usedSize()="+totalSize2);
			passed = totalSize == totalSize2;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	assertTrueWithLog("Tests totalSize() as the sum of usedSize() and availableSize()", passed);
}
 
Example #8
Source File: Cottage360.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
private SensorConnection openAccelerationSensor(){

		SensorInfo[] infos = SensorManager.findSensors("acceleration", null);
		if (infos.length==0) return null;

		// INT data type is preferred
		int i=0;
		for (i=0; i<infos.length && infos[i].getChannelInfos()[0].getDataType()!=ChannelInfo.TYPE_INT; i++);

		try{
			return i==infos.length ? (SensorConnection)Connector.open(infos[0].getUrl()):
				(SensorConnection)Connector.open(infos[i].getUrl());
		}catch (Exception e) {
			return null;
		}
	}
 
Example #9
Source File: TestSSLStreamConnection.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 #10
Source File: TestSSLStreamConnection.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 #11
Source File: TestSSLStreamConnection.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 #12
Source File: IsDirectory.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests isDirectory() on a non-existent directory URL
 */
public void test0004() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);

			boolean isDirectory = conn.isDirectory();
			addOperationDesc("isDirectory() returned " + isDirectory);
			
			passed = isDirectory==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests isDirectory() on a non-existent directory URL returns false", passed);
}
 
Example #13
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a writable file
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Setting file as writable");
			conn.setWritable(true);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests canWrite() on a writable file", passed);
}
 
Example #14
Source File: IsHidden.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests isHidden() on a non-existent file
 */
public void test0010() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting file: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean isHidden = conn.isHidden();
			addOperationDesc("isHidden() returned " + isHidden);
			
			passed = isHidden==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("isHidden() returns false for a non-existent file", passed);
}
 
Example #15
Source File: GetName.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests getName() on an existing file url
 */
public void test0003() {
	boolean passed = false;
	try {
		String url="file://"+getTestPath()+"file";
		
		FileConnection conn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
		ensureFileExists(conn);
		try {
			addOperationDesc("Using file url: " + url);
			
			String name = conn.getName();
			addOperationDesc("getName() returned " + name);

			passed = "file".equals(name);
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests getName() on an existing file url", passed);
}
 
Example #16
Source File: Exists.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests exists() on a non-existent directory
 */
public void test0004() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean exists = conn.exists();
			addOperationDesc("exists() returned " + exists);
			
			passed = exists==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests exists() on a non-existent directory", passed);
}
 
Example #17
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a directory (writable attribute is NOT supported by filesystem)
 */
public void test0006() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating directory: " + conn.getURL());
			ensureDirExists(conn);
			
			addOperationDesc("Setting directory as non-writable");
			conn.setWritable(false);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests canWrite() on a directory (writable attribute is NOT supported by filesystem)", passed);
}
 
Example #18
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() in Connector.READ mode
 */
public void test0009() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ);
		try {
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() call returned " + canWrite);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests canWrite() in Connector.READ mode", passed);
}
 
Example #19
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a non-existent file
 */
public void test0010() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting file: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("canWrite() returns false for a non-existent file", passed);
}
 
Example #20
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a non-existent file (writable attribute is NOT supported by filesystem)
 */
public void test0011() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting file: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("canWrite() returns false for a non-existent file (writable attribute is NOT supported by filesystem)", passed);
}
 
Example #21
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a non-existent directory
 */
public void test0012() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("canWrite() returns false for a non-existent directory", passed);
}
 
Example #22
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a non-existent directory (writable attribute is NOT supported by filesystem)
 */
public void test0013() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);
			
			boolean canWrite = conn.canWrite();
			addOperationDesc("canWrite() returned " + canWrite);
			
			passed = canWrite==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("canWrite() returns false for a non-existent directory (writable attribute is NOT supported by filesystem)", passed);
}
 
Example #23
Source File: FileSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests fileSize() on a file
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE); 
		try {				
			addOperationDesc("Creating file with a size of 64 bytes: " + conn.getURL());
			ensureFileExists(conn);
			ensureFileSize(conn, 64);
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
			
			passed = fileSize==64;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests fileSize() on a file", passed);
}
 
Example #24
Source File: FileSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests fileSize() on a non-existent file
 */
public void test0007() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE); 
		try {				
			addOperationDesc("Deleting file: " + conn.getURL());
			ensureNotExists(conn);
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
			
			passed = fileSize==-1;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests fileSize() on a non-existent file", passed);
}
 
Example #25
Source File: AvailableSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests availableSize() in Connector.READ mode
 */
public void test0005() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ);
		try {
			long availableSize = conn.availableSize();
			addOperationDesc("availableSize() call returned " + availableSize);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests availableSize() in Connector.READ mode", passed);
}
 
Example #26
Source File: TotalSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests totalSize()
 */
public void test0001() {
	boolean passed = false;
	try {	
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			long totalSize = conn.totalSize();
			addOperationDesc("totalSize() returned " + totalSize);
			passed = totalSize > 0;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	assertTrueWithLog("Tests totalSize()", passed);
}
 
Example #27
Source File: DirectorySize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests directorySize() in Connector.READ mode
 */
public void test0008() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ);
		try {
			long directorySize = conn.directorySize(false);
			addOperationDesc("directorySize() call returned " + directorySize);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests directorySize() in Connector.READ mode", passed);
}
 
Example #28
Source File: GetName.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests getName() on a non-existing directory url
 */
public void test0002() {
	boolean passed = false;
	try {

		String url="file://"+getTestPath()+"dir/";
					
		FileConnection conn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
		ensureNotExists(conn);
		try {
			addOperationDesc("Using directory url: " + url);
			
			String name = conn.getName();
			addOperationDesc("getName() returned " + name);
			
			passed = "dir/".equals(name);
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests getName() on a non-existing directory url", passed);
}
 
Example #29
Source File: Create.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * create() creates a new file
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting file: " + conn.getURL());
			ensureNotExists(conn);
			
			addOperationDesc("Creating file");
			conn.create();

			boolean exists = conn.exists();
			addOperationDesc("exists() returned " + exists);
			
			passed = exists==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("create() creates a new file", passed);
}
 
Example #30
Source File: Exists.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests exists() on a directory
 */
public void test0002() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating directory: " + conn.getURL());
			ensureDirExists(conn);
			
			boolean exists = conn.exists();
			addOperationDesc("exists() returned " + exists);
			
			passed = exists==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests exists() on a directory", passed);
}