Trims down obj files, so that they may be parsed faster later on. : File « File « Android






Trims down obj files, so that they may be parsed faster later on.

    

/**
  Copyright (C) 2010  Tobias Domhan

    This file is part of AndObjViewer.

    AndObjViewer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AndObjViewer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AndObjViewer.  If not, see <http://www.gnu.org/licenses/>.
 
 */
//package edu.dhbw.andobjviewer.parser;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

 class Util {
  
  private static final Pattern trimWhiteSpaces = Pattern.compile("[\\s]+");
  private static final Pattern removeInlineComments = Pattern.compile("#");
  private static final Pattern splitBySpace = Pattern.compile(" ");


  /**
   * Trims down obj files, so that they may be parsed faster later on.
   * Remove uneccessary whitespaces, comments etc.
   * @param in stream to be trimmed
   * @param out the resulting trimmed stream
   */
  public static void trim(BufferedReader in, BufferedWriter out) throws IOException {
    String line;
    out.write("#trimmed\n");
    for (line = in.readLine(); 
    line != null; 
    line = in.readLine()) {
      line = getCanonicalLine(line);
      if(line.length()>0) {
        out.write(line.trim());
        out.write('\n');
      }
    }
    in.close();
    out.close();
  }
  /**
   * returns a canonical line of a obj or mtl file.
   * e.g. it removes multiple whitespaces or comments from the given string.
   * @param line
   * @return
   */
  public static final String getCanonicalLine(String line) {
    line = trimWhiteSpaces.matcher(line).replaceAll(" ");
    if(line.contains("#")) {
      String[] parts = removeInlineComments.split(line);
      if(parts.length > 0)
        line = parts[0];//remove inline comments
    }
    return line;
  }

  
}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.File Name Extension Utils
15.Write and append string to file
16.Transfers required files to the the private file system part in order to be access them from C Code.
17.returns a canonical line of a obj or mtl file.
18.Format File Size:KB, MB, GB
19.Get File Contents as String
20.Read File and return CharSequence
21.Copy File
22.Read/write From FileSystem, browse Account
23.Create Path and file under External Storage
24.Get File Extension Name
25.Extract File Name From String
26.Write String to file
27.Create an output file from raw resources.
28.Copies a directory from a jar file to an external directory.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.