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

The following examples show how to use javax.microedition.io.file.FileConnection#openOutputStream() . 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: DirectorySize.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected void ensureFileSize(FileConnection conn, int fileSize) throws IOException {
	OutputStream os = conn.openOutputStream();
	try {
		os.write(new byte[fileSize]);
		os.flush();
	} finally {
		os.close();
	}
}
 
Example 2
Source File: OpenOutputStream_Long.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 3
Source File: Truncate.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests if a file is truncated properly even if an open outputstream is not explicitly flushed by the user 
 */
public void test0011() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream os = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Writing 8 bytes");
			os = conn.openOutputStream();
			os.write(new byte[8]);				
								
			addOperationDesc("Truncating at byte 7");
			conn.truncate(7);
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
			
			passed = fileSize==7;
		} finally {
			if (os != null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests if a file is truncated properly even if an open outputstream is not explicitly flushed by the user", passed);
}
 
Example 4
Source File: Truncate.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests truncate() where byteOffset is greater than the current end of file
 */
public void test0002() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream os = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Writing 8 bytes");
			os = conn.openOutputStream();
			os.write(new byte[8]);
			os.close();
			os = null;
			
			addOperationDesc("Truncating at byte 10");
			conn.truncate(10);
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
			
			passed = fileSize==8;
		} finally {
			if (os != null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests truncate() where byteOffset is greater than the current end of file", passed);
}
 
Example 5
Source File: Truncate.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests truncate()
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream os = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Writing 8 bytes");
			os = conn.openOutputStream();
			os.write(new byte[8]);
			os.close();
			os = null;
			
			addOperationDesc("Truncating at byte 1");
			conn.truncate(1);
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
			
			passed = fileSize==1;
		} finally {
			if (os != null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests truncate()", passed);
}
 
Example 6
Source File: OpenOutputStream.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests if the OutputStream returned from openOutputStream() updates the file immediately when flush() is called.
 */
public void test0011() {
	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);

				os = conn.openOutputStream();
									
				addOperationDesc("Writing 1024 bytes to output stream");
				os.write(new byte[1024]);
				os.flush();
				
				long fileSize2 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize2);
				
				passed = fileSize2==1024;
			} finally {
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests if the OutputStream returned from openOutputStream() updates the file immediately when flush() is called", passed);
}
 
Example 7
Source File: AvailableSize.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
public void test0001() {
	boolean passed = false;
	try {	
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream os = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			long availableSize = conn.availableSize();
			addOperationDesc("first availableSize() call returned " + availableSize);

			int incrementSize = 4096;
			byte buf[] = new byte[incrementSize];
			os = conn.openOutputStream();
			for (int i=0; i<4; i++) {
				addOperationDesc("writing " + incrementSize + " bytes to disk");
				os.write(buf);
			}
			os.close();
			os = null;

			long availableSize2 = conn.availableSize();
			addOperationDesc("second availableSize() call returned " + availableSize2 + " (should be less than first call)");
			
			passed = availableSize2<availableSize;									
		} finally {
			if (os != null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	assertTrueWithLog("Tests availableSize()", passed);
}
 
Example 8
Source File: OpenOutputStream.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests openOutputStream()
 */
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 {
				os = conn.openOutputStream();
				
				long fileSize1 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize1);
				
				addOperationDesc("Writing byte to output stream");
				os.write(3);
				addOperationDesc("Closing output stream");
				os.close();
				os = null;
				
				long fileSize2 = conn.fileSize();
				addOperationDesc("fileSize() is " + fileSize2);
				
				passed = fileSize2>fileSize1;
			} finally {
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests openOutputStream()", passed);
}
 
Example 9
Source File: UsedSize.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests usedSize()
 */
public void test0001() {
	boolean passed = false;
	try {	
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		OutputStream os = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			long usedSize = conn.usedSize();
			addOperationDesc("first usedSize() call returned " + usedSize);

			int incrementSize = 4096;
			addOperationDesc("writing " + incrementSize + " bytes to disk");
			os = conn.openOutputStream();
			os.write(new byte[incrementSize]);
			os.close();
			os = null;

			long usedSize2 = conn.usedSize();
			addOperationDesc("second usedSize() call returned " + usedSize2 + " (should be more than first call)");
			
			passed = usedSize2>usedSize;									
		} finally {
			if (os != null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	assertTrueWithLog("Tests usedSize()", passed);
}
 
Example 10
Source File: FileSize.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected void ensureFileSize(FileConnection conn, int fileSize) throws IOException {
	OutputStream os = conn.openOutputStream();
	try {
		os.write(new byte[fileSize]);
		os.flush();
	} finally {
		os.close();
	}
}
 
Example 11
Source File: FileSize.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests fileSize() on a file with an open OutputStream
 */
public void test0002() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE); 
		OutputStream os =null;
		try {				
			addOperationDesc("Creating file with a size of 0 bytes: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Opening output stream on file");
			os = conn.openOutputStream();
			addOperationDesc("Writing and flushing 64 bytes to file");
			os.write(new byte[64]);
			os.flush();
			
			long fileSize = conn.fileSize();
			addOperationDesc("fileSize() returned " + fileSize);
		
			passed = fileSize==64;
		} finally {
			if (os!=null) os.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests fileSize() on a file with an open OutputStream", passed);
}
 
Example 12
Source File: OpenInputStream.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests openInputStream()
 */
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);
			
			InputStream is = null;
			OutputStream os = null;
			try {
				addOperationDesc("Writing byte to output stream: 69");
				os = conn.openOutputStream();
				os.write(69);
				os.flush();
				
				is = conn.openInputStream();
				int result = is.read();
				addOperationDesc("Reading byte from input stream: " + result);
				
				passed = result==69;
			} finally {
				if (is != null) is.close();
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests openInputStream()", passed);
}
 
Example 13
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 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, 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 15
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 16
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 17
Source File: OpenOutputStream_Long.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests openOutputStream(long) with an offset larger than fileSize
 */
public void test0012() {
	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+3));
				os = conn.openOutputStream(fileSize1+3);
				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+7));
				os = conn.openOutputStream(fileSize2+7);
				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+2;
			} finally {
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	assertTrueWithLog("Tests openOutputStream(long) with an offset larger than fileSize", passed);
}
 
Example 18
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 19
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 20
Source File: OpenOutputStream_Long.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}