Parsing a text file on BlackBerry forever

I originally used my own RIM XML parsing methods to parse a 150 kg text file, about 5,000 xml lines, however it took about 2 minutes to complete, so I tried the line-based format:

Title: Book Title
Line 1
Line 2
Line 3

I should be able to read the file in less time than it takes to blink, but it is still slow.

Identification books are the vector of book objects, and the lines are stored in the line vector in the Book object.

class classs = Class.forName("com.Gui.FileLoader");
InputStream is = classs.getResourceAsStream( fileName );

int totalFileSize = IOUtilities.streamToBytes( is ).length;
int totalRead = 0;

//Thought that maybe a shared input stream would be faster, in this case it't not.
SharedInputStream sis = SharedInputStream.getSharedInputStream( classs.getResourceAsStream( fileName ) );

LineReader lr = new LineReader( sis );
String strLine = new String( lr.readLine() );
totalRead += strLine.length();

Book book = null;

//Loop over the file until EOF is reached, catch EOF error move on with life after that.
while(1 == 1){

    //If Line = Title: then we've got a new book add the old book to our books vector.
    if (strLine.startsWith("Title:")){

        if (book != null){
            books.addElement( book );
        }

         book = new Book();

         book.setTitle( strLine.substring( strLine.indexOf(':') + 1).trim() );

         strLine = new String( lr.readLine() );
         totalRead += strLine.length();
         continue;
    }

    int totalComplete = (int) (  ( (double) totalRead / (double) totalFileSize ) * 100.00);
    _observer.processStatusUpdate( totalComplete , book.getTitle() );

    book.addLine( strLine );

    strLine = new String( lr.readLine(), "ascii" );
    totalRead += strLine.length();
}
+3
source share
4 answers

, , , . , - , book.addLine( strLine ); , , _observer.processStatusUpdate( totalComplete , book.getTitle() );. , .

- , , BlackBerry. Eclipse . , Eclipse, "window.. show view.. other.. BlackBerry.. BlackBerry Profiler View" " " . . "" " " ", "

. , "" . .

+2

, - , . , ByteArrayInputStream? :

//Used to determine file size and then show in progress bar, app is threaded.
byte[] fileBytes = IOUtilities.streamToBytes( is );
int totalFileSize = fileBytes.length;
int totalRead = 0;

ByteArrayInputStream bais = new ByteArrayInputStream( fileBytes );
LineReader lr = new LineReader( bais);

, , , , - .

+4

new BufferedInputStream(classs.getResourceAsStream(fileName));

:

-, , , BufferedInputStream wrong.

, (doc ).

0

, ?

Unless you have a preferred profiler, there is jvisualvm in the Java 6 JDK.

(I assume that you will find all the time spent on the path down to “read the character from the file.” If so, you need to buffer)

0
source

Source: https://habr.com/ru/post/1770859/


All Articles