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

The following examples show how to use javax.microedition.io.file.FileConnection#create() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
Source File: DataInputOutputStreamFileBench.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
void runBenchmark() {
    try {
        long start;

        String dirPath = System.getProperty("fileconn.dir.private");
        FileConnection file = (FileConnection)Connector.open(dirPath + "test");
        if (file.exists()) {
            file.delete();
        }
        file.create();

        OutputStream fileOut = file.openOutputStream();
        start = JVM.monotonicTimeMillis();
        writeUTF(fileOut);
        System.out.println("DataOutputStream::writeUTF in file: " + (JVM.monotonicTimeMillis() - start));

        InputStream fileIn = file.openInputStream();
        start = JVM.monotonicTimeMillis();
        readUTF(fileIn);
        System.out.println("DataInputStream::readUTF from file: " + (JVM.monotonicTimeMillis() - start));

        file.close();
    } catch (IOException e) {
        System.out.println("Unexpected exception: " + e);
        e.printStackTrace();
    }
}
 
Example 9
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 10
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 11
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 12
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();
    }
}