001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.io.j2me.file;
028:
029: import java.util.Vector;
030: import javax.microedition.io.file.FileSystemRegistry;
031:
032: /**
033: * Caches file system roots.
034: */
035: public class RootCache {
036: /** Current root paths. */
037: private String[] roots = null;
038:
039: /** Reference to the root cache. */
040: private static RootCache instance = null;
041:
042: /** Constructs the root cache instance. */
043: private RootCache() {
044: // retrieve up-to-date mounted roots; array can not be null
045: Vector v = Protocol.listRoots();
046: roots = new String[v.size()];
047: v.copyInto(roots);
048: }
049:
050: /**
051: * Creates instance of root cache and fills it with current root paths.
052: */
053: public static synchronized void initialize() {
054: getInstance();
055: }
056:
057: /**
058: * Gets reference to the root cache.
059: * If an instance does not exist then creates it,
060: * otherwise returns existing one.
061: * @return root cache reference
062: */
063: public static synchronized RootCache getInstance() {
064: if (instance == null) {
065: instance = new RootCache();
066: }
067: return instance;
068: }
069:
070: /**
071: * Gets the list of cached roots.
072: * @return array of cached file system roots
073: */
074: public synchronized String[] getRoots() {
075: return roots;
076: }
077:
078: /**
079: * Checks if path is a root path.
080: * @param root path to be checked
081: * @return <code>true</code> if path is a root
082: */
083: public synchronized boolean isRoot(String root) {
084: for (int i = 0; i < roots.length; i++) {
085: if (root.equals(roots[i])) {
086: return true;
087: }
088: }
089: return false;
090: }
091:
092: /**
093: * Adds a root to the cache.
094: * @param root path to add to roots
095: */
096: public synchronized void addRoot(String root) {
097: String[] a = new String[roots.length + 1];
098: System.arraycopy(roots, 0, a, 0, roots.length);
099: a[roots.length] = root;
100: roots = a;
101: }
102:
103: /**
104: * Removes a root from the cache.
105: * @param root path to be removed
106: */
107: public synchronized void removeRoot(String root) {
108: int index = -1;
109: for (int i = 0; i < roots.length && index == -1; i++) {
110: if (root.equals(roots[i])) {
111: index = i;
112: }
113: }
114: if (index != -1) {
115: String[] a = new String[roots.length - 1];
116: System.arraycopy(roots, 0, a, 0, index);
117: System.arraycopy(roots, index + 1, a, index, a.length
118: - index);
119: roots = a;
120: }
121: }
122:
123: }
|