List of usage examples for java.nio MappedByteBuffer rewind
@Override public final MappedByteBuffer rewind()
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("file == null!"); }//from w ww. j a v a2s. com if (!file.canRead()) { throw new IIOException("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, DTA_HEADER_SIZE); //printHexDump(buff, "hex dump of the byte-buffer"); buff.rewind(); dbgLog.fine("applying the dta test\n"); byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); dbgLog.fine("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(hdr4)) + "<-"); if (hdr4[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((hdr4[1] != 1) && (hdr4[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!stataReleaseNumber.containsKey(hdr4[0])) { dbgLog.fine("1st byte (" + hdr4[0] + ") is not within the ingestable range [rel. 3-10]: this file is NOT stata-dta type"); return false; } else { dbgLog.fine("this file is stata-dta type: " + stataReleaseNumber.get(hdr4[0]) + "(No in HEX=" + hdr4[0] + ")"); return true; } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("file == null!"); }/* ww w . j a v a 2 s .c om*/ if (!file.canRead()) { throw new IIOException("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, DTA_HEADER_SIZE); //printHexDump(buff, "hex dump of the byte-buffer"); buff.rewind(); boolean result = false; dbgLog.fine("applying the dta test\n"); byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(hdr4)) + "<-"); if (hdr4[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((hdr4[1] != 1) && (hdr4[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!stataReleaseNumber.containsKey(hdr4[0])) { dbgLog.fine("1st byte (" + hdr4[0] + ") is not within the ingestable range [rel. 3-10]: this file is NOT stata-dta type"); return false; } else { dbgLog.fine("this file is stata-dta type: " + stataReleaseNumber.get(hdr4[0]) + "(No in HEX=" + hdr4[0] + ")"); return true; } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.sav.SAVFileReaderSpi.java
@Override public boolean canDecodeInput(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("file == null!"); }/*from w w w. jav a2 s . co m*/ if (!file.canRead()) { throw new IOException("cannot read the input file"); } dbgLog.fine("applying the sav test\n"); // 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, SAV_HEADER_SIZE); //printHexDump(buff, "hex dump of the byte-buffer"); dbgLog.info("hex dump of the 1st 4 bytes[$FL2 == 24 46 4C 32]=" + new String(Hex.encodeHex(buff.array()))); buff.rewind(); boolean DEBUG = false; byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); String hdr4sav = new String(hdr4); dbgLog.fine("from string[hdr4]=" + new String(Hex.encodeHex(hdr4)).toUpperCase()); if (hdr4sav.equals("$FL2")) { dbgLog.fine("this file is spss-sav type"); return true; } else { dbgLog.fine("this file is NOT spss-sav type"); } return false; }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReaderSpi.java
@Override public boolean canDecodeInput(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("file == null!"); }/*from w ww . ja v a2s . c o m*/ if (!file.canRead()) { throw new IOException("cannot read the input file"); } dbgLog.fine("applying the sav test\n"); // 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, SAV_HEADER_SIZE); //printHexDump(buff, "hex dump of the byte-buffer"); dbgLog.fine("hex dump of the 1st 4 bytes[$FL2 == 24 46 4C 32]=" + new String(Hex.encodeHex(buff.array()))); buff.rewind(); boolean DEBUG = false; byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); String hdr4sav = new String(hdr4); dbgLog.fine("from string[hdr4]=" + new String(Hex.encodeHex(hdr4)).toUpperCase()); if (hdr4sav.equals("$FL2")) { dbgLog.fine("this file is spss-sav type"); return true; } else { dbgLog.fine("this file is NOT spss-sav type"); } return false; }
From source file:edu.harvard.iq.dvn.ingest.dsb.SubsettableFileChecker.java
/** * test this byte buffer against SPSS-SAV spec * * */// w ww . j a va 2 s. co m public String testSAVformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; if (DEBUG) { out.println("applying the sav test\n"); } byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); String hdr4sav = new String(hdr4); if (DEBUG) { out.println("from string=" + hdr4sav); } if (hdr4sav.equals("$FL2")) { if (DEBUG) { out.println("this file is spss-sav type"); } result = "application/x-spss-sav"; } else { if (DEBUG) { out.println("this file is NOT spss-sav type"); } } return result; }
From source file:edu.harvard.iq.dvn.ingest.dsb.SubsettableFileChecker.java
/** * test this byte buffer against STATA DTA spec * *//*from w ww . java 2 s.c o m*/ public String testDTAformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; if (DEBUG) { out.println("applying the dta test\n"); } byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); if (DEBUG) { for (int i = 0; i < hdr4.length; ++i) { out.printf("%d\t%02X\n", i, hdr4[i]); } } if (hdr4[2] != 1) { if (DEBUG) { out.println("3rd byte is not 1: given file is not stata-dta type"); } return result; } else if ((hdr4[1] != 1) && (hdr4[1] != 2)) { if (DEBUG) { out.println("2nd byte is neither 0 nor 1: this file is not stata-dta type"); } return result; } else if (!SubsettableFileChecker.stataReleaseNumber.containsKey(hdr4[0])) { if (DEBUG) { out.println("1st byte (" + hdr4[0] + ") is not within the ingestable range [rel. 3-10]: this file is NOT stata-dta type"); } return result; } else { if (DEBUG) { out.println("this file is stata-dta type: " + SubsettableFileChecker.stataReleaseNumber.get(hdr4[0]) + "(No in HEX=" + hdr4[0] + ")"); } result = "application/x-stata"; } return result; }
From source file:edu.harvard.iq.dvn.ingest.dsb.SubsettableFileChecker.java
/** * test this byte buffer against SAS Transport(XPT) spec * *//*from w w w . j a va 2 s .com*/ public String testXPTformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; if (DEBUG) { out.println("applying the sas-transport test\n"); } // size test if (buff.capacity() < 91) { if (DEBUG) { out.println("this file is NOT sas-exort type\n"); } return result; } byte[] hdr1 = new byte[80]; byte[] hdr2 = new byte[11]; buff.get(hdr1, 0, 80); buff.get(hdr2, 0, 11); String hdr1st80 = new String(hdr1); String hdrnxt11 = new String(hdr2); if (DEBUG) { out.println("1st-80 bytes=" + hdr1st80); out.println("next-11 bytes=" + hdrnxt11); } if ((hdr1st80.equals(SubsettableFileChecker.SAS_XPT_HEADER_80)) && (hdrnxt11.equals(SubsettableFileChecker.SAS_XPT_HEADER_11))) { if (DEBUG) { out.println("this file is sas-export type\n"); } result = "application/x-sas-xport"; } else { if (DEBUG) { out.println("this file is NOT sas-exort type\n"); } } return result; }
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 a 2 s.c o m*/ 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.dvn.ingest.dsb.SubsettableFileChecker.java
/** * test this byte buffer against R data file * *//*from w w w. j a va 2s . c om*/ public String testRDAformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; if (DEBUG) { out.println("applying the RData test\n"); out.println("buffer capacity=" + buff.capacity()); } if (DEBUG) { byte[] rawhdr = new byte[4]; buff.get(rawhdr, 0, 4); for (int j = 0; j < 4; j++) { out.printf("%02X ", rawhdr[j]); } out.println(); buff.rewind(); } // get the first 4 bytes as an int and check its value; // if it is 0x1F8B0800, then gunzip and its first 4 bytes int magicNumber = buff.getInt(); if (DEBUG) { out.println("magicNumber in decimal =" + magicNumber); out.println("in binary=" + Integer.toBinaryString(magicNumber)); out.println("in oct=" + Integer.toOctalString(magicNumber)); out.println("in hex=" + Integer.toHexString(magicNumber)); } try { if (magicNumber == 0x1F8B0800) { if (DEBUG) { out.println("magicNumber is GZIP"); } // gunzip the first 5 bytes and check their bye-pattern // get gzip buffer size int gzip_buffer_size = this.getGzipBufferSize(buff); byte[] hdr = new byte[gzip_buffer_size]; buff.get(hdr, 0, gzip_buffer_size); GZIPInputStream gzin = new GZIPInputStream(new ByteArrayInputStream(hdr)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < RDA_HEADER_SIZE; i++) { sb.append(String.format("%02X", gzin.read())); } String fisrt5bytes = sb.toString(); result = this.checkUncompressedFirst5bytes(fisrt5bytes); // end of compressed case } else { // uncompressed case? if (DEBUG) { out.println("magicNumber is not GZIP:" + magicNumber); out.println("test as an uncompressed RData file"); } buff.rewind(); byte[] uchdr = new byte[5]; buff.get(uchdr, 0, 5); StringBuilder sb = new StringBuilder(); for (int i = 0; i < uchdr.length; i++) { sb.append(String.format("%02X", uchdr[i])); } String fisrt5bytes = sb.toString(); result = this.checkUncompressedFirst5bytes(fisrt5bytes); // end of uncompressed case } } catch (IOException ex) { ex.printStackTrace(); } return result; }
From source file:edu.harvard.iq.dataverse.ingest.IngestableDataChecker.java
/** * test this byte buffer against SPSS-SAV spec * * *///w ww . ja v a2 s. com public String testSAVformat(MappedByteBuffer buff) { String result = null; buff.rewind(); boolean DEBUG = false; // ----------------------------------------- // Avoid java.nio.BufferUnderflowException // ----------------------------------------- if (buff.capacity() < 4) { return null; } if (DEBUG) { out.println("applying the sav test\n"); } byte[] hdr4 = new byte[4]; buff.get(hdr4, 0, 4); String hdr4sav = new String(hdr4); if (DEBUG) { out.println("from string=" + hdr4sav); } if (hdr4sav.equals("$FL2")) { if (DEBUG) { out.println("this file is spss-sav type"); } result = "application/x-spss-sav"; } else { if (DEBUG) { out.println("this file is NOT spss-sav type"); } } return result; }