Java Code Examples for javax.microedition.io.file.FileConnection#exists()

The following examples show how to use javax.microedition.io.file.FileConnection#exists() . 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: Mkdir.java    From pluotsorbet with 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 2
Source File: Exists.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests exists() in Connector.READ mode
 */
public void test0007() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn1 = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		FileConnection conn2 = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ);
		try {
			addOperationDesc("Creating file: " + conn1.getURL());
			ensureFileExists(conn1);
			
			boolean exists = conn2.exists();
			addOperationDesc("exists() returned " + exists);
			
			passed = exists==true;						
		} finally {
			conn1.close();
			conn2.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests exists() in Connector.READ mode", passed);
}
 
Example 3
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 4
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);
}
 
Example 5
Source File: Exists.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests exists() 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: " + conn.getURL());
			ensureFileExists(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 file", passed);
}
 
Example 6
Source File: UTF8Bench.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
void generateData() throws IOException {
    String str = "";
    String part = "abcdefghilmnopqrstuvzABCDEFGHILMNOPQRSTUVZabcdefghilmnopqrstuvzABCDEFGHILMNOPQRSTUVZ";
    for (int i = 0; i < 2000; i++) {
      str += part;
    }

    cbuf = new char[str.length()];
    cbufReader = new char[cbuf.length];
    str.getChars(0, str.length(), cbuf, 0);

    String dirPath = System.getProperty("fileconn.dir.private");
    file = (FileConnection)Connector.open(dirPath + "test");
    if (file.exists()) {
        file.delete();
    }
    file.create();
}
 
Example 7
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 8
Source File: Delete.java    From pluotsorbet with 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 9
Source File: Delete.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests delete() 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: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Deleting file");
			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 10
Source File: GameCanvasImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
public OutputStream openOutputStream(Object connection, int offset) throws IOException {
    FileConnection fc = (FileConnection)Connector.open((String)connection, Connector.READ_WRITE);
    if(!fc.exists()) {
        fc.create();
    }
    BufferedOutputStream o = new BufferedOutputStream(fc.openOutputStream(offset), (String)connection);
    o.setConnection(fc);
    return o;
}
 
Example 11
Source File: TestCaseWithLog.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected void ensureDirExists(FileConnection conn) throws IOException {
	if (conn.exists()) {
		// if the dir already exists, delete it to ensure zero length when created again
		recursiveDelete(conn);
	}
	
	try {
		conn.mkdir();
	} catch (Exception e) {
		throw new IOException("could not create directory <" + conn.getURL() + "> (" + e.getMessage() + ")");
	}
}
 
Example 12
Source File: TestCaseWithLog.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected void ensureFileExists(FileConnection conn) throws IOException {
	if (conn.exists()) {
		// if the file already exists, delete it to ensure zero length when created again
		recursiveDelete(conn);
	}
	
	try {
		conn.create();
	} catch (Exception e) {
		throw new IOException("could not create file <" + conn.getURL() + "> (" + e.getMessage() + ")");
	}
}
 
Example 13
Source File: BlackBerryImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
public OutputStream openOutputStream(Object connection, int offset) throws IOException {
    FileConnection fc = (FileConnection) Connector.open((String) connection, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();
    }
    BufferedOutputStream o = new BufferedOutputStream(fc.openOutputStream(offset), (String) connection);
    o.setConnection(fc);
    return o;
}
 
Example 14
Source File: BlackBerryImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @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;
    }
    OutputStream os = new BlackBerryOutputStream(((HttpConnection) connection).openOutputStream());
    return new BufferedOutputStream(os, ((HttpConnection) connection).getURL());
}
 
Example 15
Source File: GameCanvasImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @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 16
Source File: TestInputOutputStorage.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public void test(TestHarness th) {
    try {
        FileConnection file = (FileConnection)Connector.open("file:////prova");
        if (file.exists()) {
            file.delete();
        }
        file.create();

        RandomAccessStream ras = new RandomAccessStream();
        ras.connect("prova", Connector.READ_WRITE);

        OutputStream out = ras.openOutputStream();

        testWrite(th, out);

        out.close();

        ras.setPosition(0);

        InputStream in = ras.openInputStream();

        testRead(th, in);

        in.close();

        ras.disconnect();

        file.delete();
        th.check(!file.exists());
        file.create();

        out = file.openOutputStream();
        testWrite(th, out);
        out.close();
        in = file.openInputStream();
        testRead(th, in);
        in.close();

        file.delete();
        file.close();
    } catch (Exception e) {
        th.fail("Unexpected exception: " + e);
        e.printStackTrace();
    }
}
 
Example 17
Source File: ContentHandlerMIDlet.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
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 18
Source File: Log.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Opens the {@link FileConnection} and a {@link PrintStream}.
 * 
 * @param filenamePrefix prefix of the filename part of the file path.
 * @param append if <code>true</code> then don't use timestamp in the filename but append to existing log file.
 * @throws IOException if a file could not be created.
 * @throws SecurityException if no permission was given to create a file.
 * @throws NullPointerException if <code>filenamePrefix</code> is <code>null</code>.
 */
private static void openFileConnection(String filenamePrefix, boolean append) 
    throws IOException, SecurityException 
{
    if (!System.getProperty("microedition.io.file.FileConnection.version").equals("1.0")) {
        // FileConnection API version 1.0 isn't supported.
        // Probably a bit unnecessary check as if it isn't supported
        // a ClassNotFoundException would have been thrown earlier.
        throw new IOException("FileConnection not available");
    }
    
    final String filename = createLogFilename(filenamePrefix, !append);
    final String[] pathProperties = {"fileconn.dir.memorycard", "fileconn.dir.recordings"};
    String path = null;
    
    // Attempt to create a file to the directories specified by the 
    // system properties in array pathProperties.
    for (int i = 0; i < pathProperties.length; i++) {
        path = System.getProperty(pathProperties[i]);
        
        // Only throw declared exceptions if this is the last path
        // to try.
        try {
            
            if (path == null) {
                
                if (i < (pathProperties.length - 1)) {
                    continue;
                } else {
                    throw new IOException("Path not available: " + pathProperties[i]);
                }
            }

            FileConnection fConn = (FileConnection) 
                Connector.open(path + filename, Connector.READ_WRITE);
            OutputStream os = null;
            
            if (append) {

                if (!fConn.exists()) {
                    fConn.create();
                }
                
                os = fConn.openOutputStream(fConn.fileSize());
            } else {
                // Assume that createLogFilename creates such a filename
                // that is enough to separate filenames even if they
                // are created in a short interval (seconds).
                fConn.create();
                os = fConn.openOutputStream();
            }
            
            streams[FILE_INDX] = new PrintStream(os);
            
            // Opening the connection and stream was successful so don't
            // try other paths.
            fileConn = fConn;
            break;
        } catch (SecurityException se) {
            if (i == (pathProperties.length - 1)) {
                throw se;
            }
        } catch (IOException ioe) {
            if (i == (pathProperties.length - 1)) {
                throw ioe;
            }                
        }
    }
}
 
Example 19
Source File: ImageProcessingBench.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
void runBenchmark() {
  try {
      long start, time = 0;

      client = (LocalMessageProtocolConnection)Connector.open("localmsg://nokia.image-processing");

      DataEncoder dataEncoder = new DataEncoder("Conv-BEB");
      dataEncoder.putStart(14, "event");
      dataEncoder.put(13, "name", "Common");
      dataEncoder.putStart(14, "message");
      dataEncoder.put(13, "name", "ProtocolVersion");
      dataEncoder.put(10, "version", "1.[0-10]");
      dataEncoder.putEnd(14, "message");
      dataEncoder.putEnd(14, "event");
      byte[] sendData = dataEncoder.getData();
      client.send(sendData, 0, sendData.length);

      LocalMessageProtocolMessage msg = client.newMessage(null);
      client.receive(msg);

      FileConnection originalImage = (FileConnection)Connector.open("file:////test.jpg", Connector.READ_WRITE);
      if (!originalImage.exists()) {
          originalImage.create();
      }
      OutputStream os = originalImage.openDataOutputStream();
      InputStream is = getClass().getResourceAsStream("/org/mozilla/io/test.jpg");
      os.write(TestUtils.read(is));
      os.close();

      MemorySampler.sampleMemory("Memory before nokia.image-processing benchmark");
      for (int i = 0; i < 1000; i++) {
        start = JVM.monotonicTimeMillis();
        String path = scaleImage();
        time += JVM.monotonicTimeMillis() - start;

        FileConnection file = (FileConnection)Connector.open(path);
        file.delete();
        file.close();
      }
      System.out.println("scaleImage: " + time);
      MemorySampler.sampleMemory("Memory after nokia.image-processing benchmark");

      originalImage.delete();
      originalImage.close();

      client.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 20
Source File: TestCaseWithLog.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
protected void ensureNotExists(FileConnection conn) throws IOException {
	if (conn.exists()) {
		recursiveDelete(conn);
	}
}