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

The following examples show how to use javax.microedition.io.file.FileConnection#close() . 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: GetName.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests getName() on an existing directory url
 */
public void test0004() {
	boolean passed = false;
	try {

		String url="file://"+getTestPath()+"dir/";
					
		FileConnection conn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
		ensureDirExists(conn);
		try {
			addOperationDesc("Using directory url: " + url);
			
			String name = conn.getName();
			addOperationDesc("getName() returned " + name);
			
			passed = "dir/".equals(name);
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests getName() on an existing directory url", passed);
}
 
Example 2
Source File: DirectorySize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 3
Source File: AvailableSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests availableSize() in Connector.READ mode
 */
public void test0005() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ);
		try {
			long availableSize = conn.availableSize();
			addOperationDesc("availableSize() call returned " + availableSize);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests availableSize() in Connector.READ mode", passed);
}
 
Example 4
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: CanWrite.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests canWrite() on a file (writable 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-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 file (writable attribute is NOT supported by filesystem)", passed);
}
 
Example 6
Source File: GetName.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests getName() on a non-existing file url
 */
public void test0001() {
	boolean passed = false;
	try {
		String url="file://"+getTestPath()+"file";
		
		FileConnection conn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
		ensureNotExists(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 a non-existing file url", passed);
}
 
Example 7
Source File: DirectorySize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests directorySize() on a non-existent directory
 */
public void test0009() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE);
		try {
			addOperationDesc("Deleting directory: " + conn.getURL());
			ensureNotExists(conn);
			
			long directorySize = conn.directorySize(true);
			addOperationDesc("directorySize(true) returned " + directorySize);
			
			passed = directorySize==-1;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests directorySize() on a non-existent directory", passed);
}
 
Example 8
Source File: TotalSize.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests totalSize() in Connector.READ mode
 */
public void test0006() {
	boolean passed = false;
	try {	
		addOperationDesc("Opening connection in READ mode");
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ);
		try {
			long totalSize = conn.totalSize();
			addOperationDesc("totalSize() call returned " + totalSize);
			
			passed = true;								
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}
	
	assertTrueWithLog("Tests totalSize() in Connector.READ mode", 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 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 10
Source File: IsHidden.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests isHidden() on a file (hidden 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 hidden");
			conn.setHidden(true);
			
			boolean isHidden = conn.isHidden();
			addOperationDesc("isHidden() returned " + isHidden + " (should have no effect)");
			
			passed = isHidden==false;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests isHidden() on a file (hidden attribute is NOT supported by filesystem)", passed);
}
 
Example 11
Source File: LastModified.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);
		try {
			
			addOperationDesc("Creating file: " + conn.getURL());
			long startTime = System.currentTimeMillis();
			ensureFileExists(conn);
			long endTime = System.currentTimeMillis();
			
			// round startTime down to nearest minute
			startTime -= startTime % 60000;
			// round endTime up to nearest minute
			endTime += 60000 - (endTime % 60000);
			
			long modifyTime = conn.lastModified();
			addOperationDesc("lastModified() returned " + modifyTime);
			
			passed = modifyTime >= startTime && modifyTime <= endTime;
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests lastModified() on a file", passed);
}
 
Example 12
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 13
Source File: GetURL.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests getURL() on an encoded file URL
 */
public void test0007() {
	boolean passed = false;
	try {

		String url="file://"+ getTestPath() +"foo%5e%25bar/file";

		String escapedUrl		= URLSupport.getEscapedForm(url);
		String alternativeUrl	= URLSupport.getAlternativeEscapedForm(url);
					
		FileConnection conn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
		try {
			addOperationDesc("Using file: " + url);
			
			String connUrl = conn.getURL();
			addOperationDesc("getURL() returned: " + connUrl);
			
			passed = connUrl.equals(escapedUrl) || connUrl.equals(alternativeUrl);
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests getURL() on an encoded file URL", passed);
}
 
Example 14
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 15
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 16
Source File: GetURL.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests if getURL() returns in escaped URL format
 */
public void test0008() {
	boolean passed = false;
	try {
		String unEscapedUrl		= "file://"+ getTestPath() +"a directory/a file";

		String escapedUrl 		= URLSupport.getEscapedForm(unEscapedUrl);
		String alternativeUrl	= URLSupport.getAlternativeEscapedForm(unEscapedUrl);
			
		FileConnection conn = (FileConnection)Connector.open(unEscapedUrl, Connector.READ_WRITE);
		try {
			addOperationDesc("Using file: " + unEscapedUrl);
			
			String connUrl = conn.getURL();
			addOperationDesc("getURL() returned: " + connUrl);
			
			passed = connUrl.equals(escapedUrl) || connUrl.equals(alternativeUrl);
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests if getURL() returns in escaped URL format", passed);
}
 
Example 17
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 18
Source File: OpenDataInputStream.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
 */
public void test0007() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		InputStream stream =null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Opening stream");
			stream = conn.openDataInputStream();
			addOperationDesc("Closing stream");
			stream.close();
			stream = null;
			
			addOperationDesc("Opening stream");
			stream = conn.openDataInputStream();
			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", passed);
}
 
Example 19
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);
}
 
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) 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);
}