Java Code Examples for javax.microedition.io.Connector

The following examples show how to use javax.microedition.io.Connector. 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: J2ME-Loader   Source File: Manager.java    License: Apache License 2.0 6 votes vote down vote up
public static Player createPlayer(String locator) throws IOException {
	if (locator == null) {
		throw new IllegalArgumentException();
	}
	if (locator.equals(MIDI_DEVICE_LOCATOR)) {
		return new MidiPlayer();
	} else if (locator.startsWith(FILE_LOCATOR)) {
		InputStream stream = Connector.openInputStream(locator);
		String extension = locator.substring(locator.lastIndexOf('.') + 1);
		String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
		return createPlayer(stream, type);
	} else if (locator.startsWith(CAPTURE_AUDIO_LOCATOR) &&
			ContextHolder.requestPermission(Manifest.permission.RECORD_AUDIO)) {
		return new RecordPlayer();
	} else {
		return new BasePlayer();
	}
}
 
Example 2
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 3
Source Project: pluotsorbet   Source File: CanRead.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canRead() on a non-readable file
 */
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);
			
			addOperationDesc("Setting file as non-readable");
			conn.setReadable(false);
			
			boolean canRead = conn.canRead();
			addOperationDesc("canRead() returned " + canRead);
			
			passed = canRead==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests canRead() on a non-readable file", passed);
}
 
Example 4
Source Project: pluotsorbet   Source File: LastModified.java    License: 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 5
Source Project: pluotsorbet   Source File: IsHidden.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests isHidden() 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 isHidden = conn.isHidden();
			addOperationDesc("isHidden() call returned " + isHidden);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests isHidden() in Connector.READ mode", passed);
}
 
Example 6
Source Project: pluotsorbet   Source File: IsHidden.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests isHidden() on a hidden 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 hidden");
			conn.setHidden(true);
			
			boolean isHidden = conn.isHidden();
			addOperationDesc("isHidden() returned " + isHidden);
			
			passed = isHidden==true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests isHidden() on a hidden file", passed);
}
 
Example 7
void benchmarkLargeRead() throws IOException {
  SocketConnection client = (SocketConnection)Connector.open("socket://localhost:8000");

  OutputStream os = client.openOutputStream();
  os.write(("GET /bench/benchmark.jar HTTP/1.1\r\n" +
            "Host: localhost\r\n" +
            "Connection: close\r\n" +
            "\r\n").getBytes());
  os.close();

  InputStream is = client.openInputStream();
  byte[] data = new byte[1024];
  int len;
  long start = JVM.monotonicTimeMillis();
  do {
    len = is.read(data);
  } while (len != -1);
  System.out.println("large read time: " + (JVM.monotonicTimeMillis() - start));
  is.close();

  client.close();
}
 
Example 8
Source Project: pluotsorbet   Source File: CanRead.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canRead() on a file (readable attribute is NOT supported by filesystem)
 */
public void test0005() {
	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 non-readable");
			conn.setReadable(false);
			
			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 file (readable attribute is NOT supported by filesystem)", passed);
}
 
Example 9
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 10
Source Project: pluotsorbet   Source File: Media.java    License: 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 11
Source Project: pluotsorbet   Source File: VideoCanvas.java    License: 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 12
Source Project: pluotsorbet   Source File: SMSMIDlet.java    License: 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 13
Source Project: pluotsorbet   Source File: TotalSize.java    License: 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 14
Source Project: pluotsorbet   Source File: Cottage360.java    License: 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 15
Source Project: pluotsorbet   Source File: Exists.java    License: 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 16
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 17
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 18
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 19
Source Project: pluotsorbet   Source File: IsDirectory.java    License: 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 20
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 21
Source Project: pluotsorbet   Source File: IsHidden.java    License: 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 22
Source Project: pluotsorbet   Source File: GetName.java    License: 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 23
Source Project: pluotsorbet   Source File: CanRead.java    License: 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 24
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 25
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 26
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 27
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 28
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 29
Source Project: pluotsorbet   Source File: CanWrite.java    License: 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 30
Source Project: pluotsorbet   Source File: FileSize.java    License: 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);
}