001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.j2me.io;
026:
027: import java.io.DataOutputStream;
028: import java.io.FileInputStream;
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.File;
034: import javax.microedition.io.Connector;
035: import com.sun.j2me.main.Configuration;
036:
037: import com.sun.j2me.security.Token;
038:
039: /**
040: * Provides abstraction for working with files
041: */
042: public class FileAccess {
043:
044: private String name;
045: private Object outputStream;
046: private Object inputStream;
047: private Token securityToken;
048:
049: public static int INTERNAL_STORAGE_ID = 0;
050:
051: /**
052: * Prevents of creating a new instance of FileAccess
053: */
054: private FileAccess() {
055: }
056:
057: private FileAccess(String name, Token securityToken) {
058: this .name = name;
059: this .securityToken = securityToken;
060: }
061:
062: /**
063: * Returns the root to build storage filenames including an needed
064: * file separators, abstracting difference of the file systems
065: * of development and device platforms. Note the root is never null.
066: *
067: * @param storageId ID of the storage the root of which should be returned
068: *
069: * @return root of any filename for accessing device persistant
070: * storage.
071: */
072: public static String getStorageRoot(int storageId) {
073: String storagePath = Configuration
074: .getProperty("sun.storage.path" + storageId)
075: + File.separator;
076: if (storagePath == null || storagePath.equals("")) {
077: throw new UnsupportedOperationException(
078: "\"sun.storage.path" + storageId
079: + "\" property is not set");
080: }
081:
082: return storagePath;
083: }
084:
085: public static FileAccess getInstance(String fileName,
086: Token securityToken) {
087: return new FileAccess(fileName, securityToken);
088: }
089:
090: public void connect(int accessType) throws IOException {
091: File file = new File(name);
092: try {
093: if ((accessType == Connector.READ)
094: || (accessType == Connector.READ_WRITE)) {
095: inputStream = new FileInputStream(file);
096: }
097:
098: if ((accessType == Connector.WRITE)
099: || (accessType == Connector.READ_WRITE)) {
100: FileOutputStream outStream = new FileOutputStream(file);
101: outputStream = new DataOutputStream(outStream);
102: }
103: } catch (FileNotFoundException fnfe) {
104: throw new IOException();
105: }
106: }
107:
108: public void disconnect() throws IOException {
109: if (inputStream instanceof InputStream) {
110: ((InputStream) inputStream).close();
111: }
112:
113: if (outputStream instanceof DataOutputStream) {
114: ((DataOutputStream) outputStream).close();
115: }
116: }
117:
118: public InputStream openInputStream() throws IOException {
119: return inputStream instanceof InputStream ? (InputStream) inputStream
120: : null;
121: }
122:
123: public DataOutputStream openDataOutputStream() throws IOException {
124: return outputStream instanceof DataOutputStream ? (DataOutputStream) outputStream
125: : null;
126: }
127:
128: public void truncate(int i) throws IOException {
129: }
130:
131: }
|