Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
pekwood
Posts: 13
Registered: ‎03-18-2010
My Device: 9550

Re: Can we read and write to a file at the same time?

thanks, rcmaniac25.

you test it under sdk5.0.

 

 

but it did not run on 4.7 or before version. the interface "Seekable" is just defined from v5.0

 

how can i use it on v4.7 or before versions?

 

 

 

-thanks for any reply.

Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Can we read and write to a file at the same time?

That is the exact thing, Seekable is 5.0 and up. You could potentially just open and close Input/OutputStream(s) multiple times. It isn't efficent but it would work.

 

The format I had done was:

  • Expected value
  • Position of value
  • Values...

I used the Seekable interface to allow me to get the expected value and position of value then to go to that position and read it. Doing it pre-Seekable would need the open-multiple-stream thing I mentioned above in order to read the expected value and position and would need to probably store the position in dedicated variables since Input and OutputStream don't have a position marker like Seekable.

 

I will see if I can modify my example and get it to work.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Can we read and write to a file at the same time?

Ok here is a 4.5 and up compatable version of my previous code. It works (admittedly I didn't test on a 4.5 device but did as many chacks as I could to prevent issues) and uses the preprocessor.

 

Code:

 

//#preprocessor

private Object lock = new Object();
private FileConnection file;
private int use = 0; //If a thread is using the stream
private boolean outerror = false; //Error in output stream
private boolean started = false; //Output stream wrote enough data for input stream to start reading
private int got = -1; //The last value the input stream got, just so that everything is in-sync and no racing occurs.

try
	{
		file = (FileConnection)Connector.open("file:///SDCard/testFile.txt", Connector.READ_WRITE);
		if(file.exists())
		{
			file.delete();
		}
		file.create();
		Thread one = new Thread(new Runnable()
		{
			public void run()
			{
				use++;
				OutputStream out = null;
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
				Seekable seek;
//#endif
				long pos = 0;
				int value = 0;
				try
				{
					out = file.openOutputStream();
					out.write(new byte[2]);
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
					seek = (Seekable)out;
//#else
					pos += 2;
//#endif
					while(true)
					{
						synchronized(lock)
						{
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
							pos = seek.getPosition();
							seek.setPosition(0);
//#else
							out.flush();
							out.close();
							out = file.openOutputStream(0);
//#endif
							out.write(value);
							System.out.println("OUT: Expect " + value);
							out.write((int)pos);
							System.out.println("OUT: At pos " + pos);
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
							seek.setPosition(pos);
//#else
							out.flush();
							out.close();
							out = file.openOutputStream(pos);
//#endif
							out.write(value);
//#ifdef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
							pos++;
//#endif
							System.out.println("OUT: Wrote " + value);
							out.flush();
							started = true;
							value++;
							if(value > 10)
							{
								break;
							}
						}
						while(got != (value - 1));
					}
				}
				catch(Exception e)
				{
					System.out.println("OUT Error: " + e);
					outerror = true;
				}
				finally
				{
					if(out != null)
					{
						try
						{
							out.close();
						}
						catch(Exception e)
						{
						}
					}
				}
				System.out.println("OUT: Done");
				fileDone();
			}
		});
		Thread two = new Thread(new Runnable()
		{
			public void run()
			{
				use++;
				InputStream in = null;
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
				Seekable seek;
//#endif
				long pos;
				try
				{
					in = file.openInputStream();
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
					seek = (Seekable)in;
//#else
					boolean seekable = in.markSupported();
					if(seekable)
					{
						in.mark(Integer.MAX_VALUE);
					}
//#endif
					while(true)
					{
						if(outerror)
						{
							//Output had an error
							throw new Exception("IN: Output had an error");
						}
						while(!started);
						synchronized(lock)
						{
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
							seek.setPosition(0);
//#else
							int p = 0;
							if(seekable)
							{
								in.reset();
							}
							else
							{
								in.close();
								in = file.openInputStream();
								if(seekable = in.markSupported())
								{
									in.mark(Integer.MAX_VALUE);
								}
							}
//#endif
							int value = in.read();
							System.out.println("IN: Expect " + value);
							pos = in.read();
							System.out.println("IN: At pos " + pos);
//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0
							seek.setPosition(pos);
//#else
							p += 2;
							while(p++ != pos){in.read();}
//#endif
							value = got = in.read();
							System.out.println("IN: Read " + value);
							if(value >= 10)
							{
								break;
							}
						}
					}
				}
				catch(Exception e)
				{
					System.out.println("IN Error: " + e);
				}
				finally
				{
					if(in != null)
					{
						try
						{
							in.close();
						}
						catch(Exception e)
						{
						}
					}
				}
				System.out.println("IN: Done");
				fileDone();
			}
		});
		one.start();
		two.start();
	}
	catch(Exception e)
	{
	}
}

private void fileDone()
{
	use--;
	if(use == 0)
	{
		try
		{
			file.close();
			file = null;
		}
		catch(Exception e)
		{
		}
	}
}

 

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.