开发者

Split/break a File in to pieces in J2ME

I need to split an audio or large imag开发者_运维技巧e file in J2ME before uploading it. How can i split/ break a file in to pieces in JavaME.


What API for file loading do you use?

1)If FileConnection API you can load data by blocks. There is no problem in this case.

2)If you use Class.getResourceAsStream( String pathInsideJar ) you will have problems. Most of KVMs load resource fully before returning control to your code. So I see one way - to split big file into several small files before creating jar.


DataInputStream dis = FileConnection.getDataInputStream();

byte[] buffer = new byte[2048];

int count;

int total = 0;

Vector v = new Vector();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while( ( count = dis.read( buffer ) ) >= 0 )

{

total += count;

baos.write( buffer, 0, count );

if( total > 100000 )

{

baos.close();

byte[] data = baos.toByteArray();

v.addElement( data );

baos = new ByteArrayOutputStream();
}

}

So you will have Vector of several byte arrays. And you can send it one by one.

Or read all file data into one byte array and post this data by parts with shifting start position of sending data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜