javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/common_j2me/org/eclipse/swt/internal/CLDCCompatibilityDelegate.java
changeset 21 2a9601315dfc
child 78 71ad690e91f5
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials 
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  * 
       
     8  * Contributors:
       
     9  *     Nokia Corporation - S60 implementation
       
    10  *******************************************************************************/
       
    11 package org.eclipse.swt.internal;
       
    12 
       
    13 import java.io.IOException;
       
    14 import java.io.InputStream;
       
    15 import java.io.OutputStream;
       
    16 
       
    17 import javax.microedition.io.Connector;
       
    18 import javax.microedition.io.file.FileConnection;
       
    19 
       
    20 public class CLDCCompatibilityDelegate implements CompatibilityDelegate {
       
    21     
       
    22     public CLDCCompatibilityDelegate(){
       
    23         super();
       
    24     }
       
    25     
       
    26     public InputStream getInputStreamFromFile(String path) throws IOException {
       
    27         String jsr75Path = convertPathToSpecific(path);
       
    28 
       
    29         InputStream inputStream = null;
       
    30         try{
       
    31             FileConnection fileConct = (FileConnection)Connector.open(jsr75Path);
       
    32             inputStream = fileConct.openInputStream();
       
    33             fileConct.close();
       
    34         }
       
    35         catch (IOException e) {
       
    36            throw e;
       
    37         }
       
    38         catch(Exception e)
       
    39         {
       
    40             throw new IOException(e.getMessage());
       
    41         }
       
    42         return inputStream;
       
    43     }
       
    44 
       
    45     public String convertPathToSpecific(String path) {
       
    46         String jsr75Path = path;
       
    47         if (!jsr75Path.startsWith("file:///")) {
       
    48             // In JSR 75 the file separator is '/' and not '\'
       
    49             jsr75Path = jsr75Path.replace('\\', '/');
       
    50             jsr75Path = "file:///" + jsr75Path;
       
    51         }
       
    52         
       
    53         return jsr75Path;
       
    54     }
       
    55 
       
    56     public String convertPathFromSpecific(String jsr75Path) {
       
    57         String stdPath = jsr75Path;
       
    58         if (jsr75Path.startsWith("file:///")) {
       
    59             stdPath = stdPath.replace('/', '\\');
       
    60             stdPath = stdPath.substring(8, stdPath.length()).replace('/', '\\');;
       
    61         }
       
    62         return stdPath;
       
    63     }
       
    64     
       
    65     public boolean canOpenFile(String path) throws SecurityException {
       
    66         String jsr75Path = convertPathToSpecific(path);
       
    67         try {
       
    68             FileConnection fileConct = (FileConnection) Connector
       
    69                     .open(jsr75Path);
       
    70             fileConct.close();
       
    71         } catch (IOException e) {
       
    72             throw new SecurityException(e.getMessage());
       
    73         }
       
    74         return true;
       
    75     }
       
    76 
       
    77     public OutputStream getOutStreamFromFile(String path) throws IOException {
       
    78         String jsr75Path = convertPathToSpecific(path);
       
    79 
       
    80         OutputStream outStream = null; 
       
    81         try{
       
    82             FileConnection fileConct = (FileConnection)Connector.open(jsr75Path);
       
    83             if (!fileConct.exists()) {
       
    84                 fileConct.create();
       
    85             }
       
    86             outStream = fileConct.openOutputStream();
       
    87             fileConct.close();
       
    88         }
       
    89         catch(IOException e)
       
    90         {
       
    91             throw e;
       
    92         }
       
    93         catch (Exception e) {
       
    94             throw new IOException(e.getMessage());
       
    95         }
       
    96         return outStream;
       
    97     }    
       
    98 
       
    99 }