Java Code Examples for javax.microedition.io.file.FileConnection#openInputStream()

The following examples show how to use javax.microedition.io.file.FileConnection#openInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GameCanvasImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
public InputStream openInputStream(Object connection) throws IOException {
    if(connection instanceof String) {
        FileConnection fc = (FileConnection)Connector.open((String)connection, Connector.READ);
        BufferedInputStream o = new BufferedInputStream(fc.openInputStream(), (String)connection);
        o.setConnection(fc);
        return o;
    }
    return new BufferedInputStream(((HttpConnection)connection).openInputStream(), ((HttpConnection)connection).getURL());
}
 
Example 2
Source File: BlackBerryImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
public InputStream openInputStream(Object connection) throws IOException {
    if (connection instanceof String) {
        FileConnection fc = (FileConnection) Connector.open((String) connection, Connector.READ);
        BufferedInputStream o = new BufferedInputStream(fc.openInputStream(), (String) connection);
        o.setConnection(fc);
        return o;
    }
    return new BufferedInputStream(((HttpConnection) connection).openInputStream(), ((HttpConnection) connection).getURL());
}
 
Example 3
Source File: DataInputOutputStreamFileBench.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
void runBenchmark() {
    try {
        long start;

        String dirPath = System.getProperty("fileconn.dir.private");
        FileConnection file = (FileConnection)Connector.open(dirPath + "test");
        if (file.exists()) {
            file.delete();
        }
        file.create();

        OutputStream fileOut = file.openOutputStream();
        start = JVM.monotonicTimeMillis();
        writeUTF(fileOut);
        System.out.println("DataOutputStream::writeUTF in file: " + (JVM.monotonicTimeMillis() - start));

        InputStream fileIn = file.openInputStream();
        start = JVM.monotonicTimeMillis();
        readUTF(fileIn);
        System.out.println("DataInputStream::readUTF from file: " + (JVM.monotonicTimeMillis() - start));

        file.close();
    } catch (IOException e) {
        System.out.println("Unexpected exception: " + e);
        e.printStackTrace();
    }
}
 
Example 4
Source File: OpenInputStream.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests openInputStream()
 */
public void test0001() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			InputStream is = null;
			OutputStream os = null;
			try {
				addOperationDesc("Writing byte to output stream: 69");
				os = conn.openOutputStream();
				os.write(69);
				os.flush();
				
				is = conn.openInputStream();
				int result = is.read();
				addOperationDesc("Reading byte from input stream: " + result);
				
				passed = result==69;
			} finally {
				if (is != null) is.close();
				if (os != null) os.close();
			}
		} finally {
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Tests openInputStream()", passed);
}
 
Example 5
Source File: OpenInputStream.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Streams can be opened and closed more than once on a connection
 */
public void test0007() {
	boolean passed = false;
	try {
		FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE);
		InputStream stream = null;
		try {
			addOperationDesc("Creating file: " + conn.getURL());
			ensureFileExists(conn);
			
			addOperationDesc("Opening stream");
			stream = conn.openInputStream();
			addOperationDesc("Closing stream");
			stream.close();
			stream = null;
			
			addOperationDesc("Opening stream");
			stream = conn.openInputStream();
			addOperationDesc("Closing stream");
			stream.close();
			stream = null;
			
			passed = true;
		} finally {
			if (stream!=null) stream.close();
			conn.close();
		}
	} catch (Exception e) {
		logUnexpectedExceptionDesc(e);
		passed = false;
	}

	assertTrueWithLog("Streams can be opened and closed more than once on a connection", passed);
}
 
Example 6
Source File: TestInputOutputStorage.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public void test(TestHarness th) {
    try {
        FileConnection file = (FileConnection)Connector.open("file:////prova");
        if (file.exists()) {
            file.delete();
        }
        file.create();

        RandomAccessStream ras = new RandomAccessStream();
        ras.connect("prova", Connector.READ_WRITE);

        OutputStream out = ras.openOutputStream();

        testWrite(th, out);

        out.close();

        ras.setPosition(0);

        InputStream in = ras.openInputStream();

        testRead(th, in);

        in.close();

        ras.disconnect();

        file.delete();
        th.check(!file.exists());
        file.create();

        out = file.openOutputStream();
        testWrite(th, out);
        out.close();
        in = file.openInputStream();
        testRead(th, in);
        in.close();

        file.delete();
        file.close();
    } catch (Exception e) {
        th.fail("Unexpected exception: " + e);
        e.printStackTrace();
    }
}