Java Code Examples for javax.microedition.io.Connector#open()

The following examples show how to use javax.microedition.io.Connector#open() . 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: pluotsorbet   File: Delete.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests delete() 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);
			
			addOperationDesc("Deleting directory");
			conn.delete();

			boolean exists = conn.exists();
			addOperationDesc("exists() returned " + exists);

			passed = exists ==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests delete() on a file", passed);
}
 
Example 2
Source Project: pluotsorbet   File: Mkdir.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);
			
			addOperationDesc("Creating directory");
			conn.mkdir();

			boolean exists = conn.exists();
			addOperationDesc("exists() returned " + exists);

			passed = exists == true;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("mkdir() creates a new directory", passed);
}
 
Example 3
public void test0004() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			
			addOperationDesc("Creating directory: " + conn.getURL());
			ensureDirExists(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 directory (modification date is NOT supported by filesystem)", passed);
}
 
Example 4
/**
 * 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 5
/**
 * Tests canRead() 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 canRead = conn.canRead();
			addOperationDesc("canRead() returned " + canRead);
			
			passed = canRead==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("canRead() returns false for a non-existent directory", passed);
}
 
Example 6
/**
 * 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 7
/**
 * 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 8
/**
 * Tests isHidden() on a non-hidden 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-hidden");
			conn.setHidden(false);
			
			boolean isHidden = conn.isHidden();
			addOperationDesc("isHidden() returned " + isHidden);
			
			passed = isHidden==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests isHidden() on a non-hidden file", passed);
}
 
Example 9
/**
 * Tests directorySize() on empty directory
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating directory: " + conn.getURL());
			ensureDirExists(conn);
			
			long directorySize = conn.directorySize(true);
			addOperationDesc("directorySize(true) returned " + directorySize);
			
			passed = directorySize==0;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests directorySize() on empty directory", passed);
}
 
Example 10
/**
 * 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 11
/**
 * 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 12
/**
 * 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 13
public void testListRoots(TestHarness th) throws IOException {
    Enumeration rootEnum = FileSystemRegistry.listRoots();
    th.check(rootEnum != null);
    while (rootEnum.hasMoreElements()) {
        ++numRoots;
        String root = (String)rootEnum.nextElement();
        FileConnection fc = (FileConnection)Connector.open("file:///" + root);
        th.check(fc != null);
    }
}
 
Example 14
/**
 * @inheritDoc
 */
public OutputStream openOutputStream(Object connection) throws IOException {
    if(connection instanceof String) {
        FileConnection fc = (FileConnection)Connector.open((String)connection, Connector.READ_WRITE);
        if(!fc.exists()) {
            fc.create();
        }
        BufferedOutputStream o = new BufferedOutputStream(fc.openOutputStream(), (String)connection);
        o.setConnection(fc);
        return o;
    }
    return new BufferedOutputStream(((HttpConnection)connection).openOutputStream(), ((HttpConnection)connection).getURL());
}
 
Example 15
public static boolean testUrl(String server, String protocol, int channel) throws IOException{

	String url = protocol + server + ":" + channel;
	String fullurl = url + ";authenticate=false;encrypt=false";
	try{
		
		Connection con = Connector.open(fullurl, Connector.READ_WRITE, true);
		PrintUtil.out.println(url + " -> Open Channel!!! " + con.getClass().getSimpleName());
		con.close();
		return true;
	}catch(IllegalArgumentException a){  
	
	}catch(Exception e){
		
		String msg = e.getMessage();
		
		if (!msg.contains("0xe00002cd") && !msg.contains("timeout")){
		
			PrintUtil.out.println(url + " -\\> " + msg);
			//e.printStackTrace();
		}
		
		return false;
	}
	
	return true;
}
 
Example 16
/**
 * Streams can be opened and closed more than once on a connection calling openOutputStream(long)
 */
public void test0007() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream stream = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Opening stream");
			stream = conn.openOutputStream(0);
			addOperationDesc("Closing stream");
			stream.close();
			stream = null;

			addOperationDesc("Opening stream");
			stream = conn.openOutputStream(0);
			addOperationDesc("Closing stream");
			stream.close();
			stream = null;
			
			passed = true;
		} finally {
			if (stream!=null) stream.close();				
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Streams can be opened and closed more than once on a connection calling openOutputStream(long)", passed);
}
 
Example 17
public Connection open(String uri, int mode, boolean timeouts)
  throws IOException
{
  if (mode != Connector.READ &&
      mode != Connector.WRITE &&
      mode != Connector.READ_WRITE)
    throw new IllegalArgumentException("Variable mode has an invalid value");

  if (null==uri || uri.length()==0)
    throw new IllegalArgumentException("URI must not be null or empty");

  int schemeSeparator = uri.indexOf(':');

  if (schemeSeparator < 0) {
    throw new IllegalArgumentException("Not a valid URI");
  }

  String scheme = uri.substring(0, schemeSeparator);

  ConnectionFactory factory = getFactory(scheme);
  Connection retval = null;

  if (factory != null) {
    retval = factory.createConnection(uri, mode, timeouts);

  } else {

    // fall back to Connector.
    try {
      retval = Connector.open(uri, mode, timeouts);
    } catch (Exception e) {
      throw new ConnectionNotFoundException();
    }

  }

  if (retval == null)
    throw new ConnectionNotFoundException();
  else
    return retval;
}
 
Example 18
public void startApp() {
    if (alreadyStarted) {
        return;
    }
    alreadyStarted = true;

    if (shouldStop()) {
        System.out.println("Test finished");
        return;
    }

    try {
        Display.getDisplay(this).setCurrent(new TestCanvas());

        // Wait MIDlet to become the foreground MIDlet
        synchronized (paintedLock) {
            while (!painted) {
                paintedLock.wait();
            }
        }

        ContentHandlerServer chServer = Registry.getServer(getClass().getName());

        // Check if the MIDlet has been invoked
        Invocation invoc = chServer.getRequest(false);
        if (invoc == null) {
            System.out.println("Invocation is null");
            return;
        }

        String shareAction = invoc.getAction();
        System.out.println("Invocation action: " + shareAction);

        String[] shareArgs = invoc.getArgs();
        for (int i = 0; i < shareArgs.length; i++) {
            System.out.println("Invocation args[" + i + "]: " + shareArgs[i]);
        }

        FileConnection image = (FileConnection)Connector.open(shareArgs[0].substring(4), Connector.READ_WRITE);
        if (!image.exists()) {
            System.out.println("Image doesn't exist");
        } else {
            System.out.println("Image exists");
            image.delete();
        }

        invoc.setArgs(null);
        chServer.finish(invoc, Invocation.INITIATED);
    } catch (Exception e) {
        System.out.println("Unexpected exception: " + e);
    }

    // Test that the Content Handler code works also if the MIDlet is already running.
    sendShareMessage();
}
 
Example 19
/**
 * Tests openOutputStream(long)
 */
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);

			OutputStream os = null;
			try {
				
				long fileSize1 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize1);
				
				addOperationDesc("Opening output stream at offset " + fileSize1);
				os = conn.openOutputStream(fileSize1);
				addOperationDesc("Writing two bytes to output stream");
				os.write(3);
				os.write(3);
				addOperationDesc("Closing output stream");
				os.close();
				
				long fileSize2 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize2);
				
				addOperationDesc("Opening output stream at offset " + (fileSize2-1));
				os = conn.openOutputStream(fileSize2-1);
				addOperationDesc("Writing two bytes to output stream");
				os.write(3);
				os.write(3);
				addOperationDesc("Closing output stream");
				os.close();
				
				long fileSize3 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize3);
				
				passed = fileSize2==fileSize1+2 && fileSize3==fileSize2+1;
			} finally {
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests openOutputStream(long)", passed);
}
 
Example 20
public static void startClient(String url) throws IOException{
	
	PrintUtil.verbose("#" + new Date() + " Waiting for connection");

	BlucatState.connection = Connector.open(url,Connector.READ_WRITE,true);
	
	PrintUtil.verbose("#Dispatching Connection");
	BlucatConnection.handle(BlucatState.connection);
	
}