/** * Copyright (c) 2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; /** * File based storage using JSR-75. */ public class FileRms implements RmsConnection { private String rootDir; private String rmsName = "FileRms"; FileConnection fc; public FileRms() { rootDir = System.getProperty("fileconn.dir.private"); } public void open() { try { fc = (FileConnection) Connector.open(rootDir + rmsName); if (!fc.exists()) { fc.create(); } } catch (IOException e) { e.printStackTrace(); } } public byte[] get() { byte[] data = null; try { InputStream is = fc.openInputStream(); int len = (int) fc.fileSize(); data = new byte[len]; int readBytes = is.read(data); if (readBytes != len) { System.out.println("read size mismatch"); } is.close(); } catch (IOException e) { e.printStackTrace(); } return data; } public void set(byte[] data) { try { OutputStream os = fc.openOutputStream(); os.write(data); os.close(); } catch (IOException e) { e.printStackTrace(); } } public void close() { try { fc.close(); } catch (IOException e) { //e.printStackTrace(); } catch (NullPointerException e) { //e.printStackTrace(); } } public void delete() { try { FileConnection file = (FileConnection) Connector.open(rootDir + rmsName); if (file.exists()) { file.delete(); } file.close(); } catch (IOException e) { e.printStackTrace(); } } }