Changeset 214


Ignore:
Timestamp:
05/12/08 14:52:50 (5 years ago)
Author:
seb
Message:

store tar index in idx file after first read. removed system.out

Location:
bbtracker/branches/map/src/org/bbtracker/mobile
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/branches/map/src/org/bbtracker/mobile/config/ConfigFile.java

    r212 r214  
    137137                FileConnection connection =  
    138138                        (FileConnection) Connector.open(url, Connector.WRITE); 
    139                 OutputStream out = connection.openOutputStream(); 
    140                 OutputStreamWriter writer = new OutputStreamWriter(out); 
    141  
    142                 Enumeration keys = params.keys(); 
    143                 Enumeration values = params.elements(); 
    144                 while (keys.hasMoreElements()) { 
    145                         String key = (String) keys.nextElement(); 
    146                         String value = (String) values.nextElement(); 
    147                          
    148                         writer.write(key); 
    149                         writer.write(' '); 
    150                         writer.write(value); 
    151                         writer.write('\n'); 
    152                 } 
    153                 writer.close(); 
    154                 out.close(); 
    155                 connection.close(); 
     139                try { 
     140                        OutputStream out = connection.openOutputStream(); 
     141                        OutputStreamWriter writer = new OutputStreamWriter(out); 
     142 
     143                        try { 
     144                                Enumeration keys = params.keys(); 
     145                                Enumeration values = params.elements(); 
     146                                while (keys.hasMoreElements()) { 
     147                                        String key = (String) keys.nextElement(); 
     148                                        String value = (String) values.nextElement(); 
     149 
     150                                        writer.write(key); 
     151                                        writer.write(' '); 
     152                                        writer.write(value); 
     153                                        writer.write('\n'); 
     154                                } 
     155                        } finally { 
     156                                writer.close(); 
     157                                out.close(); 
     158                        } 
     159                } finally { 
     160                        connection.close(); 
     161                } 
    156162        } 
    157163         
  • bbtracker/branches/map/src/org/bbtracker/mobile/gui/MapBackground.java

    r212 r214  
    1919 
    2020import java.io.ByteArrayInputStream; 
     21import java.io.DataInputStream; 
     22import java.io.DataOutputStream; 
    2123import java.io.IOException; 
    2224import java.io.InputStream; 
     
    9395        /** Constant for loading progress state. */ 
    9496        private static final int STATE_DONE = 2; 
     97 
     98        /** Constant for loading progress state. */ 
     99        private static final int STATE_ERROR = 3; 
    95100         
    96101        /** Filename. */ 
     
    149154         
    150155        /** Index in tar file. */ 
    151         private long[] tarFileIndex; 
     156        private int[] tarFileIndex; 
    152157 
    153158        /** Index in tar file. */ 
     
    283288                                if (Math.abs(self.scaleX - googleScale)  
    284289                                                < googleScale / percent) { 
    285                                         System.out.println("Using rounded google scale."); 
    286290                                        self.scaleX = googleScale; 
    287291                                        break; 
     
    333337                        paintLoadingProgress(g); 
    334338                } 
     339                paintMemoryStatus(g); 
    335340                 
    336341                g.setColor(POSITION_COLOR); 
     
    363368                        (int) ((normalisedPixelLat - normalisedPixelLatOffset) * scaleY)  
    364369                        + centerOffsetY; 
    365                 System.out.println("Offset: " + offX + "," + offY  
    366                                 + "max: " + maxTileX + ", " + maxTileY); 
    367370                boolean missingImage = false; 
    368371 
     
    373376                                        int startY = offY + y; 
    374377                                        if (startY < height && startY + tileHeight > 0) { 
    375                                                 System.out.println("Found tile."); 
    376378                                                Image img = getTileImage(x, y); 
    377379                                                if (img != null) { 
     
    395397        } 
    396398         
     399        /** 
     400         * Paint memory status. 
     401         *  
     402         * @param g graphic object 
     403         */ 
     404        private static void paintMemoryStatus(Graphics g) { 
     405                g.setColor(PROGRESS_BACKGROUND_COLOR); 
     406                  
     407                final long totalMemory = Runtime.getRuntime().totalMemory(); 
     408                final long freeMemory  = Runtime.getRuntime().freeMemory(); 
     409                final int barX = (int) (PROGRESS_BAR_WIDTH * freeMemory / totalMemory); 
     410                g.fillRect(0, PROGRESS_BAR_HEIGHT, barX, PROGRESS_BAR_HEIGHT); 
     411                g.setColor(PROGRESS_DONE_COLOR); 
     412                g.fillRect(barX, PROGRESS_BAR_HEIGHT,  
     413                                PROGRESS_BAR_WIDTH - barX, PROGRESS_BAR_HEIGHT); 
     414                g.setColor(PROGRESS_TEXT_COLOR); 
     415                StringBuffer state = new StringBuffer(); 
     416                state.append(freeMemory); 
     417                state.append('/'); 
     418                state.append(totalMemory); 
     419                g.drawString(state.toString(),  
     420                                PROGRESS_TEXT_POSITION_X,  
     421                                PROGRESS_TEXT_POSITION_Y + PROGRESS_BAR_HEIGHT,  
     422                                Graphics.TOP | Graphics.LEFT); 
     423        } 
     424 
    397425        /** 
    398426         * Paint progress bar. 
     
    421449                        state.append("Reading"); 
    422450                        break; 
     451                case STATE_DONE: 
     452                        state.append("Done"); 
     453                        break; 
    423454                default: 
    424455                        state.append("Error"); 
     
    476507                        repaint(); 
    477508                } catch (Throwable e) { 
     509                        queueState = STATE_ERROR; 
    478510                        Log.log(this, e, "loading Map Tile " + tile.fileNumber); 
    479511                } finally { 
     
    499531                                } 
    500532                        } else { 
    501                                 if (!in.markSupported()) { 
     533                                if (in != null && !in.markSupported()) { 
    502534                                        closeTarInputStream(); 
    503535                                } 
     
    523555                                } 
    524556                                if (tarFileIndex == null) { 
    525                                         readFileIndex(is); 
    526                                         if (markSupported) { 
    527                                                 is.reset(); 
    528                                         } else { 
    529                                                 closeTarInputStream(); 
    530                                                 is = openTarFileInputStream(); 
     557                                        readTarFileIndex(); 
     558                                        if (tarFileIndex == null) { 
     559                                                readFileIndex(is); 
     560                                                if (markSupported) { 
     561                                                        is.reset(); 
     562                                                } else { 
     563                                                        closeTarInputStream(); 
     564                                                        is = openTarFileInputStream(); 
     565                                                } 
     566                                                storeTarFileIndex(); 
    531567                                        } 
    532568                                } 
     
    546582                } 
    547583 
    548                 if (is != null) { 
     584                if (is != null && tarFileIndex != null) { 
    549585                        try { 
    550586                                if (markSupported) { 
     
    582618                return is; 
    583619        } 
    584          
     620 
     621        /** 
     622         * store tar file index. 
     623         *  
     624         * @throws IOException io error 
     625         */ 
     626        private void storeTarFileIndex() throws IOException { 
     627                FileConnection file =  
     628                        (FileConnection) Connector.open( 
     629                                        "file:///" + tarFileDirectory + getTarIndexFileName(),  
     630                                        Connector.WRITE); 
     631                try { 
     632                        DataOutputStream out = file.openDataOutputStream(); 
     633 
     634                        try { 
     635                                out.writeInt(tarFileIndex.length); 
     636                                for (int i = 0; i < tarFileIndex.length; i++) { 
     637                                        out.writeInt(tarFileIndex[i]); 
     638                                        out.writeInt(tarFileSize[i]); 
     639                                } 
     640                        } finally { 
     641                                out.close(); 
     642                        } 
     643                } finally { 
     644                        file.close(); 
     645                } 
     646        } 
     647 
     648        /** 
     649         * @return tar file index (replace .tar by .idx) 
     650         */ 
     651        private String getTarIndexFileName() { 
     652                int pos = tarFileName.lastIndexOf('.'); 
     653                if (pos == -1) { 
     654                        throw new RuntimeException("Invalid tar filename"); 
     655                } 
     656                return tarFileName.substring(0, pos + 1) + "idx"; 
     657        } 
     658 
     659        /** 
     660         * open and read tar file index and file sizes. 
     661         */ 
     662        private void readTarFileIndex() { 
     663                int[] tarIndex = null; 
     664                int[] tarSize  = null; 
     665                try { 
     666                        FileConnection file =  
     667                                (FileConnection) Connector.open( 
     668                                                "file:///" + tarFileDirectory + getTarIndexFileName(),  
     669                                                Connector.READ); 
     670                        DataInputStream in = file.openDataInputStream(); 
     671                        try { 
     672                                int length = in.readInt(); 
     673                                tarIndex = new int[length]; 
     674                                tarSize  = new int[length]; 
     675                                for (int i = 0; i < length; i++) { 
     676                                        tarIndex[i] = in.readInt(); 
     677                                        tarSize[i]  = in.readInt(); 
     678                                } 
     679                        } finally { 
     680                                in.close(); 
     681                        } 
     682                } catch (IOException e) { 
     683                        Log.log(this, e, "reading tar file index"); 
     684                        tarIndex = null; 
     685                } 
     686                tarFileIndex = tarIndex; 
     687                tarFileSize  = tarSize; 
     688        } 
    585689        /** 
    586690         * Read Tar file index. 
     
    593697                final int nElements = (maxTileX / tileWidth) * (maxTileY / tileHeight); 
    594698                queueTotal = nElements; 
    595                 final long[] fileIndex = new long[nElements]; 
    596                 final int[] tarSize    = new int[nElements]; 
     699                final int[] fileIndex = new int[nElements]; 
     700                final int[] tarSize   = new int[nElements]; 
    597701                long fileOffset = 0; 
    598702                final int blockSize = 512; 
     
    630734                                if (name.startsWith(baseFileName)) { 
    631735                                        final int fileNumber = parseFileNumber(name); 
    632                                         fileIndex[fileNumber] = fileOffset; 
     736                                        fileIndex[fileNumber] = (int) fileOffset; 
    633737                                        tarSize[fileNumber] = fileSize; 
    634738                                        ++imageFound; 
Note: See TracChangeset for help on using the changeset viewer.