1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: File.java 376 2005-12-29 03:14:44Z mat007 $ 29 */ 30 31 package palmed.file; 32 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import javax.microedition.io.Connector; 37 import javax.microedition.io.InputConnection; 38 import javax.microedition.io.file.FileConnection; 39 40 /*** 41 * This class represents a file on the file system. 42 * 43 * @author Mathieu Champlon 44 * @version $Revision: 376 $ $Date: 2005-12-29 12:14:44 +0900 (jeu., 29 déc. 2005) $ 45 */ 46 public final class File implements IFile 47 { 48 /*** 49 * The prefix for file connection descriptors. 50 */ 51 private static final String PREFIX = "file:///"; 52 /*** 53 * The file path. 54 */ 55 private final String path_; 56 57 /*** 58 * Create a file. 59 * 60 * @param path the absolute file path. 61 */ 62 public File( final String path ) 63 { 64 if( path == null ) 65 throw new IllegalArgumentException( "parameter 'path' is null" ); 66 path_ = path; 67 } 68 69 /*** 70 * {@inheritDoc} 71 */ 72 public InputStream openInputStream() throws IOException 73 { 74 final InputConnection connection = (InputConnection)Connector.open( PREFIX + path_, Connector.READ ); 75 final InputStream stream = connection.openInputStream(); 76 connection.close(); 77 return stream; 78 } 79 80 /*** 81 * {@inheritDoc} 82 */ 83 public OutputStream openOutputStream() throws IOException 84 { 85 final FileConnection connection = (FileConnection)Connector.open( PREFIX + path_ ); 86 if( !connection.exists() ) 87 connection.create(); 88 final OutputStream stream = connection.openOutputStream(); 89 connection.close(); 90 return stream; 91 } 92 93 /*** 94 * {@inheritDoc} 95 */ 96 public String toString() 97 { 98 return path_; 99 } 100 101 /*** 102 * {@inheritDoc} 103 */ 104 public boolean equals( final Object object ) 105 { 106 if( object instanceof File ) 107 return path_.equals( ((File)object).path_ ); 108 return false; 109 } 110 111 /*** 112 * {@inheritDoc} 113 */ 114 public int hashCode() 115 { 116 return path_.hashCode(); 117 } 118 119 /*** 120 * {@inheritDoc} 121 */ 122 public boolean exists() 123 { 124 try 125 { 126 final FileConnection connection = (FileConnection)Connector.open( PREFIX + path_, Connector.READ ); 127 final boolean exists = connection.exists(); 128 connection.close(); 129 return exists; 130 } 131 catch( IOException e ) 132 { 133 return false; 134 } 135 } 136 }