List of usage examples for java.nio MappedByteBuffer position
@Override public final MappedByteBuffer position(int newPosition)
From source file:Main.java
public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) { try {/*ww w .j a va 2s. c om*/ // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. int index = tempFileName.lastIndexOf("."); if (index != -1) tempFileName = tempFileName.substring(0, index); File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Config type = srcBitmap.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height); srcBitmap.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. srcBitmap.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. srcBitmap = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary srcBitmap.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return srcBitmap; }
From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java
/** * Source://from w w w . j a v a 2s.c om * http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource * -returns-a-mutable-bitmap-in-android-2-2-and-an-immu * * Converts a immutable bitmap to a mutable bitmap. This operation doesn't * allocates more memory that there is already allocated. * * @param imgIn * - Source image. It will be released, and should not be used * more * @return a copy of imgIn, but immutable. */ public static Bitmap convertBitmapToMutable(Bitmap imgIn) { try { // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. File file = new File(MyApp.context.getFilesDir() + File.separator + "temp.tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = imgIn.getWidth(); int height = imgIn.getHeight(); Config type = imgIn.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height); imgIn.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. imgIn.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. imgIn = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary imgIn.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temporary file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return imgIn; }
From source file:com.example.psumaps.MapView.java
public static Bitmap convertToMutable(Bitmap imgIn) { try {/*ww w. j ava 2 s. c o m*/ // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = imgIn.getWidth(); int height = imgIn.getHeight(); Bitmap.Config type = imgIn.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height); imgIn.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. imgIn.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. imgIn = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary imgIn.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return imgIn; }
From source file:pyromaniac.IO.MMFastqImporter.java
/** * Helper function for init(). Scans this.fastq file for sequence starts and records their position. * Multiple MappedByteBuffers are used to handle large files. * @throws Exceptions relating to file reading and decoding. *//*from w w w. j av a 2 s . c om*/ private void _initFile() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.fastqFile)); FileChannel fcSeq = tempStream.getChannel(); this.seqSizeLong = fcSeq.size(); this.recordStarts = new ArrayList<Pair<Integer, Long>>(); int state = -1; for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer recordBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.seqSizeLong - startPosition, HALF_GIGA)); this.recordBuffers.add(recordBuffer); int sbf_pos = this.recordBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (recordBuffer.capacity() > maxBuffer) ? maxBuffer : recordBuffer.capacity(); recordBuffer.limit(bufferSize); recordBuffer.position(0); while (recordBuffer.position() != recordBuffer.capacity()) { int prevPos = recordBuffer.position(); CharBuffer result = decoder.decode(recordBuffer); recordBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; //I see a fastq header, I am either at beginning of file, or last saw the quality line... if (curr == BEGINNING_FASTQ_SEQ && (state == -1 || state == 4)) { this.recordStarts.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile))); state = 1; } else if (curr == BEGINNING_FASTQ_QUAL && (state == 1)) { state = 2; } else if ((curr == '\n' || curr == '\r') & state == 2) { state = 3; } else if ((curr == '\n' || curr == '\r') & state == 3) { state = 4; } } int newPos = recordBuffer.limit(); if (recordBuffer.limit() + bufferSize > recordBuffer.capacity()) recordBuffer.limit(recordBuffer.capacity()); else recordBuffer.limit(recordBuffer.limit() + bufferSize); recordBuffer.position(newPos); } recordBuffer.rewind(); } }
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _init seq./*from w w w . j a va 2 s .c om*/ * * @throws Exception the exception */ private void _initSeq() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.seqFile)); FileChannel fcSeq = tempStream.getChannel(); this.seqSizeLong = fcSeq.size(); this.seqStartsLL = new ArrayList<Pair<Integer, Long>>(); for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer seqBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.seqSizeLong - startPosition, HALF_GIGA)); this.seqBuffers.add(seqBuffer); int sbf_pos = seqBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (seqBuffer.capacity() > maxBuffer) ? maxBuffer : seqBuffer.capacity(); seqBuffer.limit(bufferSize); seqBuffer.position(0); while (seqBuffer.position() != seqBuffer.capacity()) { int prevPos = seqBuffer.position(); CharBuffer result = decoder.decode(seqBuffer); seqBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; if (curr == BEGINNING_FASTA_HEADER) { seqStartsLL.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile))); } } int newPos = seqBuffer.limit(); if (seqBuffer.limit() + bufferSize > seqBuffer.capacity()) seqBuffer.limit(seqBuffer.capacity()); else seqBuffer.limit(seqBuffer.limit() + bufferSize); seqBuffer.position(newPos); } seqBuffer.rewind(); } }
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _init qual./*w ww . j av a2s . c o m*/ * * @throws Exception the exception */ private void _initQual() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.qualFile)); FileChannel fcQual = tempStream.getChannel(); this.qualSizeLong = fcQual.size(); //qual starts LL contains pairs, marking file #no (in qualBuffers) and position #no (in the buffer). this.qualStartsLL = new ArrayList<Pair<Integer, Long>>(); for (long startPosition = 0L; startPosition < this.qualSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer qualBuffer = fcQual.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.qualSizeLong - startPosition, HALF_GIGA)); //map half a gig to this channel. this.qualBuffers.add(qualBuffer); int qbf_pos = qualBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (qualBuffer.capacity() > maxBuffer) ? maxBuffer : qualBuffer.capacity(); qualBuffer.limit(bufferSize); qualBuffer.position(0); while (qualBuffer.position() != qualBuffer.capacity()) { int prevPos = qualBuffer.position(); CharBuffer result = decoder.decode(qualBuffer); qualBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; if (curr == BEGINNING_FASTA_HEADER) { qualStartsLL.add(new Pair<Integer, Long>(qbf_pos, new Long(posInFile))); } } int newPos = qualBuffer.limit(); if (qualBuffer.limit() + bufferSize > qualBuffer.capacity()) qualBuffer.limit(qualBuffer.capacity()); else qualBuffer.limit(qualBuffer.limit() + bufferSize); qualBuffer.position(newPos); } qualBuffer.rewind(); } }
From source file:org.apache.hadoop.hdfs.hoss.db.FileBlockStore.java
/** * map a block(4KB) or a segment(256KB)//from w w w . j a v a 2 s .co m * @param index * the index of block * @param useSegments * whether use segmeng? * @return the mapping Buffer */ @SuppressWarnings("unchecked") public final MappedByteBuffer getMmapForIndex(final int index, boolean useSegments) { if (!validState) throw new InvalidStateException(); final int mapIdx = (useSegments ? addressIndexToSegment(index) : index); final int mapSize = (useSegments ? segmentSize : blockSize); try { final Reference<MappedByteBuffer> bref = mmaps.get(mapIdx); MappedByteBuffer mbb = null; if (bref != null) { mbb = bref.get(); } if (mbb == null) { // Create mmap final long mapOffset = ((long) mapIdx * mapSize); mbb = fileChannel.map(FileChannel.MapMode.READ_WRITE, mapOffset, mapSize); // mbb.load(); mmaps.put(mapIdx, new BufferReference<MappedByteBuffer>(mapIdx, mbb)); } else { mbb.clear(); } if (useSegments) { // slice segment final int sliceBegin = (addressIndexToSegmentOffset(index) * blockSize); final int sliceEnd = (sliceBegin + blockSize); mbb.limit(sliceEnd); mbb.position(sliceBegin); mbb = (MappedByteBuffer) mbb.slice(); } return mbb; } catch (IOException e) { LOG.error("IOException in getMmapForIndex(" + index + ")", e); } return null; }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java
@Override public boolean canDecodeInput(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("file == null!"); }// ww w . ja v a2 s. c om if (!file.canRead()) { throw new IOException("cannot read the input file"); } // set-up a FileChannel instance for a given file object FileChannel srcChannel = new FileInputStream(file).getChannel(); // create a read-only MappedByteBuffer MappedByteBuffer buff = srcChannel.map(FileChannel.MapMode.READ_ONLY, 0, POR_HEADER_SIZE); //printHexDump(buff, "hex dump of the byte-buffer"); buff.rewind(); boolean DEBUG = false; dbgLog.fine("applying the spss-por test\n"); // size test if (buff.capacity() < 491) { dbgLog.fine("this file is NOT spss-por type"); return false; } //windows [0D0A]=> [1310] = [CR/LF] //unix [0A] => [10] //mac [0D] => [13] // 3char [0D0D0A]=> [131310] spss for windows rel 15 // expected results // unix case: [0A] : [80], [161], [242], [323], [404], [485] // windows case: [0D0A] : [81], [163], [245], [327], [409], [491] // : [0D0D0A] : [82], [165], [248], [331], [414], [495] buff.rewind(); byte[] nlch = new byte[36]; int pos1; int pos2; int pos3; int ucase = 0; int wcase = 0; int mcase = 0; int three = 0; int nolines = 6; int nocols = 80; for (int i = 0; i < nolines; ++i) { int baseBias = nocols * (i + 1); // 1-char case pos1 = baseBias + i; buff.position(pos1); dbgLog.finer("\tposition(1)=" + buff.position()); int j = 6 * i; nlch[j] = buff.get(); if (nlch[j] == 10) { ucase++; } else if (nlch[j] == 13) { mcase++; } // 2-char case pos2 = baseBias + 2 * i; buff.position(pos2); dbgLog.finer("\tposition(2)=" + buff.position()); nlch[j + 1] = buff.get(); nlch[j + 2] = buff.get(); // 3-char case pos3 = baseBias + 3 * i; buff.position(pos3); dbgLog.finer("\tposition(3)=" + buff.position()); nlch[j + 3] = buff.get(); nlch[j + 4] = buff.get(); nlch[j + 5] = buff.get(); dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]); dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]); if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) { three++; } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) { wcase++; } buff.rewind(); } if (three == nolines) { dbgLog.fine("0D0D0A case"); windowsNewLine = false; } else if ((ucase == nolines) && (wcase < nolines)) { dbgLog.fine("0A case"); windowsNewLine = false; } else if ((ucase < nolines) && (wcase == nolines)) { dbgLog.fine("0D0A case"); } else if ((mcase == nolines) && (wcase < nolines)) { dbgLog.fine("0D case"); windowsNewLine = false; } buff.rewind(); int PORmarkPosition = POR_MARK_POSITION_DEFAULT; if (windowsNewLine) { PORmarkPosition = PORmarkPosition + 5; } else if (three == nolines) { PORmarkPosition = PORmarkPosition + 10; } byte[] pormark = new byte[8]; buff.position(PORmarkPosition); buff.get(pormark, 0, 8); String pormarks = new String(pormark); dbgLog.fine("pormark =>" + pormarks + "<-"); if (pormarks.equals(POR_MARK)) { dbgLog.fine("this file is spss-por type"); return true; } else { dbgLog.fine("this file is NOT spss-por type"); } return false; }
From source file:edu.harvard.iq.dataverse.ingest.IngestableDataChecker.java
/** * test this byte buffer against SPSS Portable (POR) spec * *//*from w w w. ja va2 s .c o m*/ public String testPORformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; if (DEBUG) { out.println("applying the spss-por test\n"); } // size test int bufferCapacity = buff.capacity(); dbgLog.fine("Subsettable Checker: buffer capacity: " + bufferCapacity); if (bufferCapacity < 491) { if (DEBUG) { out.println("this file is NOT spss-por type\n"); } return result; } //windows [0D0A]=> [1310] = [CR/LF] //unix [0A] => [10] //mac [0D] => [13] // 3char [0D0D0A]=> [131310] spss for windows rel 15 // expected results // unix case: [0A] : [80], [161], [242], [323], [404], [485] // windows case: [0D0A] : [81], [163], [245], [327], [409], [491] // : [0D0D0A] : [82], [165], [248], [331], [414], [495] buff.rewind(); byte[] nlch = new byte[36]; int pos1; int pos2; int pos3; int ucase = 0; int wcase = 0; int mcase = 0; int three = 0; int nolines = 6; int nocols = 80; for (int i = 0; i < nolines; ++i) { int baseBias = nocols * (i + 1); // 1-char case pos1 = baseBias + i; if (pos1 > bufferCapacity - 1) { dbgLog.fine("Subsettable Checker: request to go beyond buffer capacity (" + pos1 + ")"); return result; } buff.position(pos1); if (DEBUG) { out.println("\tposition(1)=" + buff.position()); } int j = 6 * i; nlch[j] = buff.get(); if (nlch[j] == 10) { ucase++; } else if (nlch[j] == 13) { mcase++; } // 2-char case pos2 = baseBias + 2 * i; if (pos2 > bufferCapacity - 2) { dbgLog.fine("Subsettable Checker: request to read 2 bytes beyond buffer capacity (" + pos2 + ")"); return result; } buff.position(pos2); if (DEBUG) { out.println("\tposition(2)=" + buff.position()); } nlch[j + 1] = buff.get(); nlch[j + 2] = buff.get(); // 3-char case pos3 = baseBias + 3 * i; if (pos3 > bufferCapacity - 3) { dbgLog.fine("Subsettable Checker: request to read 3 bytes beyond buffer capacity (" + pos3 + ")"); return result; } buff.position(pos3); if (DEBUG) { out.println("\tposition(3)=" + buff.position()); } nlch[j + 3] = buff.get(); nlch[j + 4] = buff.get(); nlch[j + 5] = buff.get(); if (DEBUG) { out.println(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]); out.println(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]); } if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) { three++; } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) { wcase++; } buff.rewind(); } if (three == nolines) { if (DEBUG) { out.println("0D0D0A case"); } windowsNewLine = false; } else if ((ucase == nolines) && (wcase < nolines)) { if (DEBUG) { out.println("0A case"); } windowsNewLine = false; } else if ((ucase < nolines) && (wcase == nolines)) { if (DEBUG) { out.println("0D0A case"); } } else if ((mcase == nolines) && (wcase < nolines)) { if (DEBUG) { out.println("0D case"); } windowsNewLine = false; } buff.rewind(); int PORmarkPosition = POR_MARK_POSITION_DEFAULT; if (windowsNewLine) { PORmarkPosition = PORmarkPosition + 5; } else if (three == nolines) { PORmarkPosition = PORmarkPosition + 10; } byte[] pormark = new byte[8]; buff.position(PORmarkPosition); buff.get(pormark, 0, 8); String pormarks = new String(pormark); if (DEBUG) { out.println("pormark =>" + pormarks + "<-"); } if (pormarks.equals(POR_MARK)) { if (DEBUG) { out.println("this file is spss-por type"); } result = "application/x-spss-por"; } else { if (DEBUG) { out.println("this file is NOT spss-por type"); } } return result; }
From source file:gephi.spade.panel.fcsFile.java
/** * readFile ---//from ww w .j a v a 2 s . c o m * <p> * A helper function to read all the fields in the TEXT segment of the FCS * file. * </p> * * <p> * This helper function should only be called once by the constructor as it * is quite expensive. * </p> * * @param extractEventsP * boolean flag indicating whether to extract events in the * underlying file. * @throws <code>java.io.FileNotFoundException</code> if the file is not * found. * @throws <code>java.io.IOException</code> if an IO exception occurred. */ private void readFile(boolean extractEventsP) throws FileNotFoundException, IOException { // Open a file input stream to the file FileInputStream fis = new FileInputStream(file); // Create a byte array to hold the version byte[] versionArray = new byte[VERSION_SIZE]; // Read the version into the byte array int numRead = fis.read(versionArray); if (numRead < VERSION_SIZE) { // If the number of bytes read is less than the number of bytes in // the version string, then the file is too small to be an FCS file. isFCSP = false; // Close the file input stream fis.close(); // Quit return; } // Decode the version using the default encoding version = new String(versionArray); // Determine whether the file is an FCS file by whether the version // string starts with the FCS_PREFIX isFCSP = version.startsWith(FCS_PREFIX); if (!isFCSP) { // If the file is not an FCS file, then close the file and quit. // Close the file input stream fis.close(); // Quit return; } /** * At this point, we are pretty sure that the file is an FCS file. So, * we parse it. */ /** * Get the standard HEADER stuff */ // Skip 4 bytes to get to byte 10 fis.skip(4); // Create a byte array to hold the HEADER byte[] headerArray = new byte[48]; // Read the header into the byte array numRead = fis.read(headerArray); if (numRead < 48) { // If the number of bytes read is less than 48, then the file is too // small to be an FCS file. isFCSP = false; // Close the file input stream fis.close(); // Quit return; } try { // Try to parse the TEXT segment start and end and DATA segment // start and end textStart = Integer.parseInt((new String(headerArray, 0, 8)).trim()); textEnd = Integer.parseInt((new String(headerArray, 8, 8)).trim()); dataStart = Integer.parseInt((new String(headerArray, 16, 8)).trim()); dataEnd = Integer.parseInt((new String(headerArray, 24, 8)).trim()); } catch (NumberFormatException nfe) { // If a NumberFormatException occured, then quit because there's // nothing we can do without the TEXT or DATA segment. // Close the file input stream fis.close(); return; } /** * Get the ANALYSIS segment limits */ try { // Try to parse the analysisStart and analysisEnd analysisStart = Integer.parseInt((new String(headerArray, 32, 8)).trim()); analysisEnd = Integer.parseInt((new String(headerArray, 40, 8)).trim()); } catch (NumberFormatException nfe) { // If a NumberFormatException occured, then set the ANALYSIS start // and end to 0 since this segment is optional. analysisStart = 0; analysisEnd = 0; } /** * Use NIO to read the OTHER and TEXT segments */ // Get the channel for the input file FileChannel fc = fis.getChannel(); // Move the channel's position back to 0 fc.position(0); // Map the TEXT segment to memory MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, textEnd + 1); /** * Create the character decoder for parsing characters */ decoder = charset.newDecoder(); /** * Get the OTHER segment */ mbb.limit(textStart); mbb.position(58); CharBuffer other = decoder.decode(mbb.slice()); /** * Get the TEXT segment */ mbb.limit(textEnd + 1); mbb.position(textStart); text = decoder.decode(mbb.slice()).toString(); /** * Close the file since we have the string version of the TEXT segment */ // Close the file channel fc.close(); // Close the file input stream fis.close(); /** * Decode the TEXT segment */ // The first character of the primary TEXT segment contains the // delimiter character delimiter = text.charAt(0); /** * Key/Value Pairs */ // Generate all the pairs String[] pairs; if (delimiter == '\\') { // If the delimiter character is a backslash, then we have to escape // it in the regular expression. pairs = text.split("[\\\\]"); } else { // Otherwise, we can just split it normally by using the character // in the regular expression. pairs = text.split("[" + Character.toString(delimiter) + "]"); } /** * Calculate the number of pairs --- The number of pairs is the length * of the pairs array minus 1 divided by 2. The one is due to the empty * first element from the Java split above. */ int numPairs = (pairs.length - 1) / 2; // Create a mapping for each key and its value settings = new Properties(); // Loop through the TEXT segment we just split to get the keys and // values // The key is in (i * 2) + 1 to account for the empty first element. // The value is in (i * 2) + 2 to account for the empty first element. for (int i = 0; i < numPairs; i++) { settings.setProperty(pairs[(i * 2) + 1].trim(), pairs[(i * 2) + 2].trim()); } // Go through all the key/value pairs and parse them parseSettings(); /** * Extract Events */ if (extractEventsP) { // If we are extracting data, then do so. extractEvents(); } }