Package javax.microedition.io.file

Examples of javax.microedition.io.file.FileConnection


      String subject = (String)args[1].toString();
      String body = (String)args[2].toString();
      String attachment = (String)args[3].toString();
      String fName = attachment;
      byte[] data = null;
      FileConnection fconn = null;
      DataInputStream is = null;
      try{
      fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
      is = fconn.openDataInputStream();            
      data = IOUtilities.streamToBytes(is);
          is.close();
          fconn.close();
      } catch (Exception ex) {
         Dialog.inform("Error in file path: " + ex.toString());
         return new Boolean(false);
      }
      //create a multipart
View Full Code Here


    }
  }
 
  private static void createFolder(String path) throws Exception
  {
    FileConnection fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);
   
    if (!fc.exists()) {
      fc.mkdir();
      fc.setWritable(true);
    }
   
    fc.close();
  }
View Full Code Here

   
    fc.close();
  }

  public void run() {
    FileConnection fc = null;
    HttpConnection hc = null;
   
    InputStream is = null;
    OutputStream os = null;
   
    boolean isHTTPS = false;
    boolean hasQuery = false;
   
    StringBuffer errorBuffer;
   
    try {
     
      try {
        createFolders(_filePath);
       
                fc = (FileConnection)Connector.open(_filePath, Connector.READ_WRITE);
               
                if (!fc.exists()) {
                  fc.create();
                }
            } catch (Exception e) {
              errorBuffer = new StringBuffer("Unable to create file: ");
              errorBuffer.append(e.getMessage());
             
              Logger.error(errorBuffer.toString());
                callErrorCallback(new String[] { errorBuffer.toString() });
                return;
            }
           
      if (_url.indexOf("https") == 0) {
        isHTTPS = true;
        Logger.error("Setting End to End");
        _factory.setEndToEndDesired(isHTTPS);
      }
     
      hasQuery = (_url.indexOf("?") > -1);
     
      String params = (_params != null) ? getParameters(_params) : ""
     
      if (HttpConnection.GET == _method && params.length() > 0) {
        if (hasQuery) {
          _url += "&" + params;
        } else {
          _url += "?" + params;
        }
      }
           
      ConnectionDescriptor connDesc = _factory.getConnection(_url);
     
      if (connDesc != null) {
        Logger.info("URL: " + connDesc.getUrl());
        try {
          if (isHTTPS) {
            hc = (HttpsConnection) connDesc.getConnection();
          } else {
            hc = (HttpConnection) connDesc.getConnection();
          }   
       
          hc.setRequestMethod(_method);
         
          if (_headers != null) {
            String hKey;
            String hVal;
            for (Enumeration e = _headers.keys(); e.hasMoreElements();) {
              hKey = e.nextElement().toString();
                    hVal = (String) _headers.get(hKey);
                   
                    Logger.error(hKey +": " + hVal);

              hc.setRequestProperty(hKey, hVal);
            }
          }
         
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_USER_AGENT,
                        System.getProperty("browser.useragent"));
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_KEEP_ALIVE, "300");
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONNECTION, "keep-alive");
         
          if (HttpConnection.POST == _method) {
            os = hc.openDataOutputStream();
            os.write(params.getBytes());
          }
               
                is = hc.openDataInputStream();
                int responseCode = hc.getResponseCode();
               
          if (responseCode != HttpConnection.HTTP_OK) {
            Logger.error("Response code: " +responseCode);
            callErrorCallback(new Object[] { "Server Error", new Integer(responseCode) });
          } else {
            byte[] file = IOUtilities.streamToBytes(is);
                  os = fc.openDataOutputStream();
                  os.write(file);
                  os.close();
            callSuccessCallback(new Object[]{ _filePath });
          }
        } catch (Throwable e) {
          callErrorCallback(new String[] { e.getMessage() });
          e.printStackTrace();
        }
      } else {
        Logger.error("Error creating HTTP connection");
        callErrorCallback(new String[] { "Error creating HTTP connection." });
      }
           
    } finally {
       try {
        if (fc != null) fc.close();
        if (os != null) os.close();
        if (is != null) is.close();
        if (hc != null) hc.close();
       } catch (Exception e) {
       }
View Full Code Here

  private boolean saveBitmap(Bitmap image, String filePath)
  {
    boolean result = false;
    try
    {
      FileConnection fconn = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
      if (!fconn.exists()) fconn.create();

      if (image != null)
      {
        PNGEncodedImage encodedImage = PNGEncodedImage.encode(image);
        byte[] imageBytes = encodedImage.getData();
        OutputStream out = fconn.openOutputStream();
        out.write(imageBytes);
        out.close();
        result = true;
      }

      fconn.close();
    }
    catch (Exception e)
    {
      System.out.println("Exception in CaptureScreenshot.saveBitmap: " + e.toString());
      e.getMessage();
View Full Code Here

         
          private final String[] ALLOWED_EXTENSIONS = {"jpq","jpeg","png"};

            public void run() {

              FileConnection fconn = null;
              try{
                  MultiFormatWriter writer = new MultiFormatWriter();

                  int width = 50;
                  int height = 50;
                  BarcodeFormat format = BarcodeFormat.QR_CODE;
                  Hashtable hints = new Hashtable(2);
                  String filePath = "file:///SDCard/";
                  String fileName = ""+(new Date()).getTime();
                  String fileExtension = "png";
                 
                  if(options != null){
                    Object  widthObj =options.getField("width");
                    if(widthObj != UNDEFINED){
                      width =((Integer)widthObj).intValue();
                    }
                   
                    Object heightObj = options.getField("height");
                    if(heightObj != UNDEFINED){
                      height = ((Integer)heightObj).intValue();
                    }
                   
                    Object formatObj = options.getField("format");
                    if(formatObj != UNDEFINED){
                      format = BarcodeFormat.valueOf(((String)formatObj).toUpperCase());
                    }                   
                   
                    //Hints
                    Object charSetObj = options.getField("characterSet");
                    if(charSetObj != UNDEFINED){
                      hints.put(EncodeHintType.CHARACTER_SET, (String)charSetObj);
                    }
                    Object errorCorrectionObj = options.getField("errorCorrection");
                    if(errorCorrectionObj != UNDEFINED){
                      hints.put(EncodeHintType.ERROR_CORRECTION, (Integer)errorCorrectionObj);
                    }
                   
                    Object filePathObj = options.getField("filePath");
                    if(filePathObj != UNDEFINED){
                      String filePathOrig = (String)filePathObj;
                      fileExtension = filePathOrig.substring(filePathOrig.lastIndexOf('.')+1);
                      fileName = filePathOrig.substring(filePathOrig.lastIndexOf('/')+1,filePathOrig.lastIndexOf('.'));
                      filePath = filePathOrig.substring(0, filePathOrig.lastIndexOf('/'));
                     
                      boolean validExtension = false;
                      for( int i =0; i< ALLOWED_EXTENSIONS.length;i++){
                        validExtension |= fileExtension.equals(ALLOWED_EXTENSIONS[i]);
                      }
                      if(!validExtension){
                        fileExtension = "png";
                      }
                    }                                                         
                   
                  }
                 
                  ByteMatrix bm;                 
                  try{
                     bm = writer.encode(contents, format, width, height,hints);
                  }catch(ClassCastException cce){
                    throw new Exception("ZXing Barcode Writer FAILURE - try different paramteres and pray");
                  }
                  //Bitmap b = BarcodeBitmap.createBitmap(bm,0);
                  Bitmap b = new Bitmap(width,height);

                  byte[][] array = bm.getArray();
                  //iterate through the matrix and draw the pixels to the image
                  //a Jeff and BT super hack to make generating a barcode work
                  for (int y = 0; y < height; y++) {
                    int[] argbArray = new int[width];
                   for (int x = 0; x < width; x++) {
                    int grayValue = array[y][x] & 0xff;
                    int pixel = (grayValue == 0 ? 0xFF000000 : 0xFFFFFFFF);
                    argbArray[x] = pixel;
                   }
                   b.setARGB(argbArray,0, width, 0,y, width, 1);
                  }
                 
                 


                 
                fconn = (FileConnection)Connector.open(filePath+"/"+fileName+"."+fileExtension, Connector.READ_WRITE);

                    if(fconn.exists()){
                      fileName += (new Date()).getTime();                     
                      fconn.close();
                      fconn = (FileConnection)Connector.open(filePath+"//"+fileName+"."+fileExtension, Connector.READ_WRITE);
                    }
        
                    fconn.create();

                    OutputStream outputStream = fconn.openOutputStream();
                    EncodedImage encodedImage;
                    if(fileExtension.equals("jpg") || fileExtension.equals("jpeg")){
                      encodedImage = JPEGEncodedImage.encode(b, 100);
                    }else{
                      encodedImage = PNGEncodedImage.encode(b);
                    }
                    byte[] imageBytes = encodedImage.getData();
                    outputStream.write(imageBytes);
                    outputStream.close();
                   
                    fconn.close();
                    Object[] args = {filePath+"//"+fileName+"."+fileExtension};
                    generatededCallback.invoke(thiz, args);
                 
              }catch(Exception e){               
                throwError(e);
              }finally{
          try {
            fconn.close();
          } catch (IOException e) {
            throwError(e);
          }
              }
View Full Code Here

   
    new Thread(this).start();
  }

  public void run() {
    FileConnection fc = null;
    HttpConnection hc = null;
   
    InputStream is = null;
    OutputStream os = null;
   
    boolean isHTTPS = false;
   
    try {
     
      try {
                fc = (FileConnection)Connector.open(_filePath, Connector.READ);
            } catch (Exception e) {
              Logger.error("Invalid file path");
                callErrorCallback(new String[] {"Invalid file path"});
                return;
            }
           
            Logger.log("Setting mime type...");
           
      if (_mimeType == null) {
                _mimeType = MIMETypeAssociations.getMIMEType(_filePath);
                if (_mimeType == null) {
                    _mimeType = HttpProtocolConstants.CONTENT_TYPE_IMAGE_JPEG;
                }         
            }
           
            if (!fc.exists()) {
              Logger.error("File not found");
              callErrorCallback(new String[] { _filePath + " not found" });
            }
           
      if (_url.indexOf("https") == 0) {
        isHTTPS = true;
        Logger.error("Setting End to End");
        _factory.setEndToEndDesired(isHTTPS);
      }
           
      ConnectionDescriptor connDesc = _factory.getConnection(_url);
     
      if (connDesc != null) {
        Logger.info("URL: " + connDesc.getUrl());
        try {
          if (isHTTPS) {
            hc = (HttpsConnection) connDesc.getConnection();
          } else {
            hc = (HttpConnection) connDesc.getConnection();
          }
         
          String startBoundary = getStartBoundary(_fileKey, fc.getName(), _mimeType);
          String endBoundary = getEndBoundary();
         
          String params = (_params != null) ? getParameters(_params) : "";
         
          long fileSize = fc.fileSize();
          long contentLength = fileSize +
                  (long)startBoundary.length() +
                  (long)endBoundary.length() +
                  (long)params.length();
       
       
          hc.setRequestMethod(HttpConnection.POST);
         
          if (_headers != null) {
            String hKey;
            String hVal;
            for (Enumeration e = _headers.keys(); e.hasMoreElements();) {
              hKey = e.nextElement().toString();
                    hVal = (String) _headers.get(hKey);
                   
                    Logger.error(hKey +": " + hVal);

              hc.setRequestProperty(hKey, hVal);
            }
          }
         
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_USER_AGENT,
                        System.getProperty("browser.useragent"));
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_KEEP_ALIVE, "300");
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONNECTION, "keep-alive");
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONTENT_TYPE,
                        HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
          hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                        Long.toString(contentLength));
         
         
          os = hc.openDataOutputStream();
         
          os.write(params.getBytes());
          os.write(startBoundary.getBytes());
         
          is = fc.openInputStream();
                byte[] data = IOUtilities.streamToBytes(is);
                os.write(data);
                is.close();
               
                os.write(endBoundary.getBytes());
                os.flush();
                os.close();
               
                is = hc.openDataInputStream();
                int responseCode = hc.getResponseCode();
               
          if (responseCode != HttpConnection.HTTP_OK) {
            Logger.error("Response code: " +responseCode);
            callErrorCallback(new Object[] { "Server Error", new Integer(responseCode) });
          } else {
            callSuccessCallback(new Object[]{ new String(IOUtilities.streamToBytes(is)) });
          }
        } catch (Throwable e) {
          callErrorCallback(new String[] { e.getMessage() });
          e.printStackTrace();
        }
      } else {
        Logger.error("Error creating HTTP connection");
        callErrorCallback(new String[] { "Error creating HTTP connection." });
      }
           
    } finally {
       try {
        if (fc != null) fc.close();
        if (os != null) os.close();
        if (is != null) is.close();
        if (hc != null) hc.close();
       } catch (Exception e) {
       }
View Full Code Here

            conn.close();
        }
    }

    public void removeFile(String fileName) throws IOException {
        final FileConnection conn = (FileConnection) Connector.open(rootPrefix + fileName);
        try {
            if (conn.exists()) {
                conn.delete();
            }
        } finally {
            conn.close();
        }
    }
View Full Code Here

            String root = (String) rootEnum.nextElement();
            if (root.endsWith("/")) {
                root = root.substring(0, root.length() - 1);
            }
            try {
                FileConnection fc = (FileConnection) Connector.open("file:///" + root + PATH);
                if (fc.exists()) {
                    rootPrefix = "file:///" + root + PATH;
                    break;
                }
            } catch (SecurityException ex) {
            } catch (IllegalArgumentException ex) {
View Full Code Here

    }

    public boolean existsData() throws IOException {
        if (rootPrefix == null)
            return false;
        final FileConnection conn = (FileConnection) Connector.open(rootPrefix + "list");
        try {
            return conn.exists();
        } finally {
            conn.close();
        }
    }
View Full Code Here

        }
    }

    public StoredFileInfo getFileInfo(final String fileName) throws IOException {
        final StoredFileInfo result = new StoredFileInfo();
        final FileConnection conn = (FileConnection) Connector.open(rootPrefix + fileName);
        try {
            if (conn.exists()) {
                result.fileLength = conn.fileSize();
                final InputStream in = conn.openInputStream();
                try {
                    final byte[] data = BaseStorage.readFromInputStream(in);
                    result.fileCRC = CRC32.calculate(data);
                } finally {
                    in.close();
                }
            } else {
                result.fileLength = -1;
            }
        } finally {
            conn.close();
        }
        return result;
    }
View Full Code Here

TOP

Related Classes of javax.microedition.io.file.FileConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.