Java源码示例:javax.microedition.io.Connector

示例1
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();
	}
}
 
示例2
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;
}
 
示例3
/**
 * 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);
}
 
示例4
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);
}
 
示例5
/**
 * 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);
}
 
示例6
/**
 * 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);
}
 
示例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();
}
 
示例8
/**
 * 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);
}
 
示例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());
}
 
示例10
/**
 * 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;
}
 
示例11
/**
 * 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;
}
 
示例12
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();
    }
}
 
示例13
/**
 * 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);
}
 
示例14
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;
		}
	}
 
示例15
/**
 * 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);
}
 
示例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();
    }
}
 
示例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();
        }
    }
}
 
示例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();
    }
}
 
示例19
/**
 * 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);
}
 
示例20
/**
 * 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);
}
 
示例21
/**
 * 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);
}
 
示例22
/**
 * 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);
}
 
示例23
/**
 * 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);
}
 
示例24
/**
 * 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);
}
 
示例25
/**
 * 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);
}
 
示例26
/**
 * 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);
}
 
示例27
/**
 * 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);
}
 
示例28
/**
 * 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);
}
 
示例29
/**
 * 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);
}
 
示例30
/**
 * 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);
}