List of usage examples for java.nio ByteBuffer getLong
public abstract long getLong();
From source file:org.apache.hadoop.hbase.io.hfile.TestHFileBlockIndex.java
@Test public void testSecondaryIndexBinarySearch() throws IOException { int numTotalKeys = 99; assertTrue(numTotalKeys % 2 == 1); // Ensure no one made this even. // We only add odd-index keys into the array that we will binary-search. int numSearchedKeys = (numTotalKeys - 1) / 2; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(numSearchedKeys);/*w w w. j a v a 2s.c o m*/ int curAllEntriesSize = 0; int numEntriesAdded = 0; // Only odd-index elements of this array are used to keep the secondary // index entries of the corresponding keys. int secondaryIndexEntries[] = new int[numTotalKeys]; for (int i = 0; i < numTotalKeys; ++i) { byte[] k = TestHFileWriterV2.randomOrderedKey(rand, i * 2); KeyValue cell = new KeyValue(k, Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("val")); //KeyValue cell = new KeyValue.KeyOnlyKeyValue(k, 0, k.length); keys.add(cell.getKey()); String msgPrefix = "Key #" + i + " (" + Bytes.toStringBinary(k) + "): "; StringBuilder padding = new StringBuilder(); while (msgPrefix.length() + padding.length() < 70) padding.append(' '); msgPrefix += padding; if (i % 2 == 1) { dos.writeInt(curAllEntriesSize); secondaryIndexEntries[i] = curAllEntriesSize; LOG.info(msgPrefix + "secondary index entry #" + ((i - 1) / 2) + ", offset " + curAllEntriesSize); curAllEntriesSize += cell.getKey().length + HFileBlockIndex.SECONDARY_INDEX_ENTRY_OVERHEAD; ++numEntriesAdded; } else { secondaryIndexEntries[i] = -1; LOG.info(msgPrefix + "not in the searched array"); } } // Make sure the keys are increasing. for (int i = 0; i < keys.size() - 1; ++i) assertTrue(KeyValue.COMPARATOR.compare(new KeyValue.KeyOnlyKeyValue(keys.get(i), 0, keys.get(i).length), new KeyValue.KeyOnlyKeyValue(keys.get(i + 1), 0, keys.get(i + 1).length)) < 0); dos.writeInt(curAllEntriesSize); assertEquals(numSearchedKeys, numEntriesAdded); int secondaryIndexOffset = dos.size(); assertEquals(Bytes.SIZEOF_INT * (numSearchedKeys + 2), secondaryIndexOffset); for (int i = 1; i <= numTotalKeys - 1; i += 2) { assertEquals(dos.size(), secondaryIndexOffset + secondaryIndexEntries[i]); long dummyFileOffset = getDummyFileOffset(i); int dummyOnDiskSize = getDummyOnDiskSize(i); LOG.debug("Storing file offset=" + dummyFileOffset + " and onDiskSize=" + dummyOnDiskSize + " at offset " + dos.size()); dos.writeLong(dummyFileOffset); dos.writeInt(dummyOnDiskSize); LOG.debug("Stored key " + ((i - 1) / 2) + " at offset " + dos.size()); dos.write(keys.get(i)); } dos.writeInt(curAllEntriesSize); ByteBuffer nonRootIndex = ByteBuffer.wrap(baos.toByteArray()); for (int i = 0; i < numTotalKeys; ++i) { byte[] searchKey = keys.get(i); byte[] arrayHoldingKey = new byte[searchKey.length + searchKey.length / 2]; // To make things a bit more interesting, store the key we are looking // for at a non-zero offset in a new array. System.arraycopy(searchKey, 0, arrayHoldingKey, searchKey.length / 2, searchKey.length); KeyValue.KeyOnlyKeyValue cell = new KeyValue.KeyOnlyKeyValue(arrayHoldingKey, searchKey.length / 2, searchKey.length); int searchResult = BlockIndexReader.binarySearchNonRootIndex(cell, nonRootIndex, KeyValue.COMPARATOR); String lookupFailureMsg = "Failed to look up key #" + i + " (" + Bytes.toStringBinary(searchKey) + ")"; int expectedResult; int referenceItem; if (i % 2 == 1) { // This key is in the array we search as the element (i - 1) / 2. Make // sure we find it. expectedResult = (i - 1) / 2; referenceItem = i; } else { // This key is not in the array but between two elements on the array, // in the beginning, or in the end. The result should be the previous // key in the searched array, or -1 for i = 0. expectedResult = i / 2 - 1; referenceItem = i - 1; } assertEquals(lookupFailureMsg, expectedResult, searchResult); // Now test we can get the offset and the on-disk-size using a // higher-level API function.s boolean locateBlockResult = (BlockIndexReader.locateNonRootIndexEntry(nonRootIndex, cell, KeyValue.COMPARATOR) != -1); if (i == 0) { assertFalse(locateBlockResult); } else { assertTrue(locateBlockResult); String errorMsg = "i=" + i + ", position=" + nonRootIndex.position(); assertEquals(errorMsg, getDummyFileOffset(referenceItem), nonRootIndex.getLong()); assertEquals(errorMsg, getDummyOnDiskSize(referenceItem), nonRootIndex.getInt()); } } }
From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinBlockReader.java
/** * Parses the Bitcoin transactions in a byte buffer. * * @param rawByteBuffer ByteBuffer from which the transactions have to be parsed * @param noOfTransactions Number of expected transactions * * @return Array of transactions//w w w. j ava 2s .c om * * */ public List<BitcoinTransaction> parseTransactions(ByteBuffer rawByteBuffer, long noOfTransactions) { ArrayList<BitcoinTransaction> resultTransactions = new ArrayList<BitcoinTransaction>( new Long(noOfTransactions).intValue()); // read all transactions from ByteBuffer for (int k = 0; k < noOfTransactions; k++) { // read version int currentVersion = rawByteBuffer.getInt(); // read inCounter byte[] currentInCounterVarInt = BitcoinUtil.convertVarIntByteBufferToByteArray(rawByteBuffer); long currentNoOfInputs = BitcoinUtil.getVarInt(currentInCounterVarInt); // read inputs ArrayList<BitcoinTransactionInput> currentTransactionInput = new ArrayList<BitcoinTransactionInput>( new Long(currentNoOfInputs).intValue()); for (int i = 0; i < currentNoOfInputs; i++) { // read previous Hash of Transaction byte[] currentTransactionInputPrevTransactionHash = new byte[32]; rawByteBuffer.get(currentTransactionInputPrevTransactionHash, 0, 32); // read previousTxOutIndex long currentTransactionInputPrevTxOutIdx = BitcoinUtil .convertSignedIntToUnsigned(rawByteBuffer.getInt()); // read InScript length (Potential Internal Exceed Java Type) byte[] currentTransactionTxInScriptLengthVarInt = BitcoinUtil .convertVarIntByteBufferToByteArray(rawByteBuffer); long currentTransactionTxInScriptSize = BitcoinUtil .getVarInt(currentTransactionTxInScriptLengthVarInt); // read inScript int currentTransactionTxInScriptSizeInt = new Long(currentTransactionTxInScriptSize).intValue(); byte[] currentTransactionInScript = new byte[currentTransactionTxInScriptSizeInt]; rawByteBuffer.get(currentTransactionInScript, 0, currentTransactionTxInScriptSizeInt); // read sequence no long currentTransactionInputSeqNo = BitcoinUtil.convertSignedIntToUnsigned(rawByteBuffer.getInt()); // add input currentTransactionInput.add(new BitcoinTransactionInput(currentTransactionInputPrevTransactionHash, currentTransactionInputPrevTxOutIdx, currentTransactionTxInScriptLengthVarInt, currentTransactionInScript, currentTransactionInputSeqNo)); } // read outCounter byte[] currentOutCounterVarInt = BitcoinUtil.convertVarIntByteBufferToByteArray(rawByteBuffer); long currentNoOfOutput = BitcoinUtil.getVarInt(currentOutCounterVarInt); // read outputs ArrayList<BitcoinTransactionOutput> currentTransactionOutput = new ArrayList<BitcoinTransactionOutput>( new Long(currentNoOfOutput).intValue()); for (int i = 0; i < currentNoOfOutput; i++) { // read value long currentTransactionOutputValue = rawByteBuffer.getLong(); // read outScript length (Potential Internal Exceed Java Type) byte[] currentTransactionTxOutScriptLengthVarInt = BitcoinUtil .convertVarIntByteBufferToByteArray(rawByteBuffer); long currentTransactionTxOutScriptSize = BitcoinUtil .getVarInt(currentTransactionTxOutScriptLengthVarInt); int currentTransactionTxOutScriptSizeInt = new Long(currentTransactionTxOutScriptSize).intValue(); // read outScript byte[] currentTransactionOutScript = new byte[currentTransactionTxOutScriptSizeInt]; rawByteBuffer.get(currentTransactionOutScript, 0, currentTransactionTxOutScriptSizeInt); currentTransactionOutput.add(new BitcoinTransactionOutput(currentTransactionOutputValue, currentTransactionTxOutScriptLengthVarInt, currentTransactionOutScript)); } // lock_time int currentTransactionLockTime = rawByteBuffer.getInt(); // add transaction resultTransactions .add(new BitcoinTransaction(currentVersion, currentInCounterVarInt, currentTransactionInput, currentOutCounterVarInt, currentTransactionOutput, currentTransactionLockTime)); } return resultTransactions; }
From source file:org.neo4j.io.pagecache.PageCacheTest.java
@Test(timeout = SHORT_TIMEOUT_MILLIS) public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception { getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL); PagedFile pagedFile = pageCache.map(file("a"), 20); try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) { assertTrue(cursor.next());/*from www. j av a 2 s .c om*/ byte[] data = { 42, 43, 44, 45, 46 }; cursor.putLong(41); // 0+8 = 8 cursor.putInt(41); // 8+4 = 12 cursor.putShort((short) 41); // 12+2 = 14 cursor.putByte((byte) 41); // 14+1 = 15 cursor.putBytes(data); // 15+5 = 20 } try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) { assertTrue(cursor.next()); long a = cursor.getLong(); // 8 int b = cursor.getInt(); // 12 short c = cursor.getShort();// 14 byte[] data = new byte[] { cursor.getByte(), // 15 cursor.getByte(), // 16 cursor.getByte(), // 17 cursor.getByte(), // 18 cursor.getByte(), // 19 cursor.getByte() // 20 }; cursor.setOffset(0); cursor.putLong(1 + a); cursor.putInt(1 + b); cursor.putShort((short) (1 + c)); for (byte d : data) { d++; cursor.putByte(d); } } pagedFile.close(); StoreChannel channel = fs.open(file("a"), "r"); ByteBuffer buf = ByteBuffer.allocate(20); channel.read(buf); buf.flip(); assertThat(buf.getLong(), is(42L)); assertThat(buf.getInt(), is(42)); assertThat(buf.getShort(), is((short) 42)); assertThat(buf.get(), is((byte) 42)); assertThat(buf.get(), is((byte) 43)); assertThat(buf.get(), is((byte) 44)); assertThat(buf.get(), is((byte) 45)); assertThat(buf.get(), is((byte) 46)); assertThat(buf.get(), is((byte) 47)); }
From source file:au.org.ala.layers.intersect.Grid.java
public float[] getGrid() { int maxArrayLength = Integer.MAX_VALUE - 10; if (grid_data != null) { return grid_data; }/*w w w. ja v a 2s .c o m*/ Grid loadedAlready = getLoadedGrid(filename); if (loadedAlready != null && loadedAlready.grid_data != null) { return loadedAlready.grid_data; } int length = nrows * ncols; float[] ret = new float[length]; RandomAccessFile afile = null; File f2 = new File(filename + ".GRI"); try { //read of random access file can throw an exception if (!f2.exists()) { afile = new RandomAccessFile(filename + ".gri", "r"); } else { afile = new RandomAccessFile(filename + ".GRI", "r"); } byte[] b = new byte[(int) Math.min(afile.length(), maxArrayLength)]; int i = 0; int max = 0; int len; while ((len = afile.read(b)) > 0) { ByteBuffer bb = ByteBuffer.wrap(b); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } if (datatype.equalsIgnoreCase("UBYTE")) { max += len; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.get(); if (ret[i] < 0) { ret[i] += 256; } } } else if (datatype.equalsIgnoreCase("BYTE")) { max += len; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.get(); } } else if (datatype.equalsIgnoreCase("SHORT")) { max += len / 2; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.getShort(); } } else if (datatype.equalsIgnoreCase("INT")) { max += len / 4; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.getInt(); } } else if (datatype.equalsIgnoreCase("LONG")) { max += len / 8; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.getLong(); } } else if (datatype.equalsIgnoreCase("FLOAT")) { max += len / 4; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = bb.getFloat(); } } else if (datatype.equalsIgnoreCase("DOUBLE")) { max += len / 8; max = Math.min(max, ret.length); for (; i < max; i++) { ret[i] = (float) bb.getDouble(); } } else { // / should not happen; catch anyway... max += len / 4; for (; i < max; i++) { ret[i] = Float.NaN; } } } //replace not a number for (i = 0; i < length; i++) { if ((float) ret[i] == (float) nodatavalue) { ret[i] = Float.NaN; } else { ret[i] *= rescale; } } } catch (Exception e) { logger.error("An error has occurred - probably a file error", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } grid_data = ret; return ret; }
From source file:au.org.ala.layers.intersect.Grid.java
public void replaceValues(Map<Integer, Integer> translation) { long length = ((long) nrows) * ((long) ncols); Integer minv = null;/*ww w .j a v a 2 s . co m*/ Integer maxv = null; for (Integer i : translation.values()) { if (minv == null || i < minv) minv = i; if (maxv == null || i > maxv) maxv = i; } RandomAccessFile afile = null; RandomAccessFile out = null; File f2 = new File(filename + ".GRI"); File newGrid = new File(filename + ".gri.new"); try { //read of random access file can throw an exception out = new RandomAccessFile(newGrid, "rw"); if (!f2.exists()) { afile = new RandomAccessFile(filename + ".gri", "r"); } else { afile = new RandomAccessFile(filename + ".GRI", "r"); } byte[] b = new byte[65536]; byte[] bout = new byte[65536]; long i = 0; long max = 0; long len; float v; float ndv = (float) nodatavalue; while ((len = afile.read(b)) > 0) { ByteBuffer bb = ByteBuffer.wrap(b); ByteBuffer bbout = ByteBuffer.wrap(bout); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); bbout.order(ByteOrder.LITTLE_ENDIAN); } if (datatype.equalsIgnoreCase("UBYTE")) { throw new Exception("UBYTE translation not supported"); } else if (datatype.equalsIgnoreCase("BYTE")) { throw new Exception("BYTE translation not supported"); } else if (datatype.equalsIgnoreCase("SHORT")) { max += len / 2; max = Math.min(max, length); for (; i < max; i++) { v = bb.getShort(); if (v != ndv && translation.get((int) (v * rescale)) == null) { v = v; } if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putShort((short) v); } } else if (datatype.equalsIgnoreCase("INT")) { max += len / 4; max = Math.min(max, length); for (; i < max; i++) { v = bb.getInt(); if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putInt((int) v); } } else if (datatype.equalsIgnoreCase("LONG")) { max += len / 8; max = Math.min(max, length); for (; i < max; i++) { v = bb.getLong(); if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putLong((long) v); } } else if (datatype.equalsIgnoreCase("FLOAT")) { throw new Exception("FLOAT translation not supported"); } else if (datatype.equalsIgnoreCase("DOUBLE")) { throw new Exception("DOUBLE translation not supported"); } else { max += len / 4; for (; i < max; i++) { // should not happen; catch anyway... } } out.write(bout, 0, (int) len); } writeHeader(filename + ".new", xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols, minv, maxv, datatype, nodatavalue + ""); } catch (Exception e) { logger.error("An error has occurred getting grid class stats", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } if (out != null) { try { out.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } try { if (!new File(filename + ".gri.old").exists()) FileUtils.moveFile(new File(filename + ".gri"), new File(filename + ".gri.old")); if (!new File(filename + ".grd.old").exists()) FileUtils.moveFile(new File(filename + ".grd"), new File(filename + ".grd.old")); FileUtils.moveFile(new File(filename + ".gri.new"), new File(filename + ".gri")); FileUtils.moveFile(new File(filename + ".new.grd"), new File(filename + ".grd")); } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
private boolean checkVersion(FileChannel channel) throws IOException { if (channel.size() > 0) { channel.position(0);/*from ww w . ja va 2 s. c o m*/ ByteBuffer buffer; if (useNIOMemoryMapping) { MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, 8); mbb.load(); buffer = mbb; } else { buffer = ByteBuffer.wrap(new byte[8]); channel.read(buffer); buffer.position(0); } buffer.position(0); long onDiskVersion = buffer.getLong(); return (version == onDiskVersion); } return (version == 0); }
From source file:au.org.ala.layers.intersect.Grid.java
public void getClassInfo(Map<Float, float[]> info) { long length = ((long) nrows) * ((long) ncols); RandomAccessFile afile = null; File f2 = new File(filename + ".GRI"); try { //read of random access file can throw an exception if (!f2.exists()) { afile = new RandomAccessFile(filename + ".gri", "r"); } else {//from w w w . ja v a 2 s . c om afile = new RandomAccessFile(filename + ".GRI", "r"); } byte[] b = new byte[65536]; long i = 0; long max = 0; long len; float v; float ndv = (float) nodatavalue; while ((len = afile.read(b)) > 0) { ByteBuffer bb = ByteBuffer.wrap(b); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } if (datatype.equalsIgnoreCase("UBYTE")) { max += len; max = Math.min(max, length); for (; i < max; i++) { v = bb.get(); if (v < 0) v += 256; if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("BYTE")) { max += len; max = Math.min(max, length); for (; i < max; i++) { v = bb.get(); if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("SHORT")) { max += len / 2; max = Math.min(max, length); for (; i < max; i++) { v = bb.getShort(); if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("INT")) { max += len / 4; max = Math.min(max, length); for (; i < max; i++) { v = bb.getInt(); if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("LONG")) { max += len / 8; max = Math.min(max, length); for (; i < max; i++) { v = bb.getLong(); if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("FLOAT")) { max += len / 4; max = Math.min(max, length); for (; i < max; i++) { v = bb.getFloat(); if (v != ndv) updatesStats(info, i, v * rescale); } } else if (datatype.equalsIgnoreCase("DOUBLE")) { max += len / 8; max = Math.min(max, length); for (; i < max; i++) { v = (float) bb.getDouble(); if (v != ndv) updatesStats(info, i, v * rescale); } } else { max += len / 4; for (; i < max; i++) { // should not happen; catch anyway... } } } } catch (Exception e) { logger.error("An error has occurred getting grid class stats", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } }
From source file:au.org.ala.layers.intersect.Grid.java
/** * Increase sampleEveryNthPoint to return a smaller grid. * * Grid max and min values may be skipped. * * This does not used previously cached data. * * @param sampleEveryNthPoint/* ww w . ja va 2 s . com*/ * @return */ public float[] getGrid(int sampleEveryNthPoint) { int maxArrayLength = Integer.MAX_VALUE - 10; if (subgrids != null) { //sample points int size = 1000; double[][] points = new double[size * size][2]; int pos = 0; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { points[pos][0] = xmin + (xmax - xmin) * j / (double) size; points[pos][1] = ymax - (ymax - ymin) * i / (double) size; pos++; } } return getValues3(points, 64); } int length = (nrows / sampleEveryNthPoint) * (ncols); float[] ret = new float[length]; RandomAccessFile afile = null; File f2 = new File(filename + ".GRI"); try { //read of random access file can throw an exception if (!f2.exists()) { afile = new RandomAccessFile(filename + ".gri", "r"); } else { afile = new RandomAccessFile(filename + ".GRI", "r"); } int sz = (int) Math.min(afile.length() / sampleEveryNthPoint / sampleEveryNthPoint, maxArrayLength); sz += 8 - sz % 8; byte[] b = new byte[sz]; long i = 0; long max = 0; int len; while ((len = afile.read(b)) > 0) { ByteBuffer bb = ByteBuffer.wrap(b); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } if (datatype.equalsIgnoreCase("UBYTE")) { max += len; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.get(); if (ret[(int) (i / sampleEveryNthPoint)] < 0) { ret[(int) (i / sampleEveryNthPoint)] += 256; } } } else if (datatype.equalsIgnoreCase("BYTE")) { max += len; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.get(); } } else if (datatype.equalsIgnoreCase("SHORT")) { max += len / 2; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.getShort(); } } else if (datatype.equalsIgnoreCase("INT")) { max += len / 4; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.getInt(); } } else if (datatype.equalsIgnoreCase("LONG")) { max += len / 8; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.getLong(); } } else if (datatype.equalsIgnoreCase("FLOAT")) { max += len / 4; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / sampleEveryNthPoint)] = bb.getFloat(); } } else if (datatype.equalsIgnoreCase("DOUBLE")) { max += len / 8; max = Math.min(max, ret.length * (long) sampleEveryNthPoint); for (; i < max; i++) { ret[(int) (i / (long) sampleEveryNthPoint)] = (float) bb.getDouble(); } } else { // / should not happen; catch anyway... max += len / 4; for (; i < max; i++) { ret[(int) (i / (long) sampleEveryNthPoint)] = Float.NaN; } } } //replace not a number for (i = 0; i < length; i++) { if ((float) ret[(int) i] == (float) nodatavalue) { ret[(int) i] = Float.NaN; } else { ret[(int) i] *= rescale; } } } catch (Exception e) { logger.error("An error has occurred - probably a file error", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } grid_data = ret; return ret; }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
private void setStatusFromFile(FileChannel channel) throws IOException { if (channel.size() > 0) { channel.position(0);//w w w . j ava2 s . c o m ByteBuffer buffer; if (useNIOMemoryMapping) { MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, channel.size()); mbb.load(); buffer = mbb; } else { buffer = ByteBuffer.wrap(new byte[(int) channel.size()]); channel.read(buffer); buffer.position(0); } buffer.position(0); long onDiskVersion = buffer.getLong(); if (version != onDiskVersion) { CRC32 crc32 = new CRC32(); crc32.update((int) (onDiskVersion >>> 32) & 0xFFFFFFFF); crc32.update((int) (onDiskVersion >>> 0) & 0xFFFFFFFF); int size = buffer.getInt(); crc32.update(size); LinkedHashMap<String, IndexEntry> newIndexEntries = new LinkedHashMap<String, IndexEntry>(); // Not all state is saved some is specific to this index so we // need to add the transient stuff. // Until things are committed they are not shared unless it is // prepared for (int i = 0; i < size; i++) { String indexTypeString = readString(buffer, crc32); IndexType indexType; try { indexType = IndexType.valueOf(indexTypeString); } catch (IllegalArgumentException e) { throw new IOException("Invalid type " + indexTypeString); } String name = readString(buffer, crc32); String parentName = readString(buffer, crc32); String txStatus = readString(buffer, crc32); TransactionStatus status; try { status = TransactionStatus.valueOf(txStatus); } catch (IllegalArgumentException e) { throw new IOException("Invalid status " + txStatus); } String mergeId = readString(buffer, crc32); long documentCount = buffer.getLong(); crc32.update((int) (documentCount >>> 32) & 0xFFFFFFFF); crc32.update((int) (documentCount >>> 0) & 0xFFFFFFFF); long deletions = buffer.getLong(); crc32.update((int) (deletions >>> 32) & 0xFFFFFFFF); crc32.update((int) (deletions >>> 0) & 0xFFFFFFFF); byte deleteOnlyNodesFlag = buffer.get(); crc32.update(deleteOnlyNodesFlag); boolean isDeletOnlyNodes = deleteOnlyNodesFlag == 1; if (!status.isTransient()) { newIndexEntries.put(name, new IndexEntry(indexType, name, parentName, status, mergeId, documentCount, deletions, isDeletOnlyNodes)); } } long onDiskCRC32 = buffer.getLong(); if (crc32.getValue() == onDiskCRC32) { for (IndexEntry entry : indexEntries.values()) { if (entry.getStatus().isTransient()) { newIndexEntries.put(entry.getName(), entry); } } version = onDiskVersion; indexEntries = newIndexEntries; } else { throw new IOException("Invalid file check sum"); } } } }
From source file:srebrinb.compress.sevenzip.SevenZFile.java
private void readFilesInfo(final ByteBuffer header, final Archive archive) throws IOException { final long numFiles = readUint64(header); final SevenZArchiveEntry[] files = new SevenZArchiveEntry[(int) numFiles]; for (int i = 0; i < files.length; i++) { files[i] = new SevenZArchiveEntry(); }//from ww w . j a va2 s. c o m BitSet isEmptyStream = null; BitSet isEmptyFile = null; BitSet isAnti = null; while (true) { final int propertyType = getUnsignedByte(header); if (propertyType == 0) { break; } final long size = readUint64(header); switch (propertyType) { case NID.kEmptyStream: { isEmptyStream = readBits(header, files.length); break; } case NID.kEmptyFile: { if (isEmptyStream == null) { // protect against NPE throw new IOException("Header format error: kEmptyStream must appear before kEmptyFile"); } isEmptyFile = readBits(header, isEmptyStream.cardinality()); break; } case NID.kAnti: { if (isEmptyStream == null) { // protect against NPE throw new IOException("Header format error: kEmptyStream must appear before kAnti"); } isAnti = readBits(header, isEmptyStream.cardinality()); break; } case NID.kName: { final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("Not implemented"); } if (((size - 1) & 1) != 0) { throw new IOException("File names length invalid"); } final byte[] names = new byte[(int) (size - 1)]; header.get(names); int nextFile = 0; int nextName = 0; for (int i = 0; i < names.length; i += 2) { if (names[i] == 0 && names[i + 1] == 0) { files[nextFile++].setName(new String(names, nextName, i - nextName, CharsetNames.UTF_16LE)); nextName = i + 2; } } if (nextName != names.length || nextFile != files.length) { throw new IOException("Error parsing file names"); } break; } case NID.kCTime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasCreationDate(timesDefined.get(i)); if (files[i].getHasCreationDate()) { files[i].setCreationDate(header.getLong()); } } break; } case NID.kATime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasAccessDate(timesDefined.get(i)); if (files[i].getHasAccessDate()) { files[i].setAccessDate(header.getLong()); } } break; } case NID.kMTime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasLastModifiedDate(timesDefined.get(i)); if (files[i].getHasLastModifiedDate()) { files[i].setLastModifiedDate(header.getLong()); } } break; } case NID.kWinAttributes: { final BitSet attributesDefined = readAllOrBits(header, files.length); final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasWindowsAttributes(attributesDefined.get(i)); if (files[i].getHasWindowsAttributes()) { files[i].setWindowsAttributes(header.getInt()); } } break; } case NID.kStartPos: { throw new IOException("kStartPos is unsupported, please report"); } case NID.kDummy: { // 7z 9.20 asserts the content is all zeros and ignores the property // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 if (skipBytesFully(header, size) < size) { throw new IOException("Incomplete kDummy property"); } break; } default: { // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 if (skipBytesFully(header, size) < size) { throw new IOException("Incomplete property of type " + propertyType); } break; } } } int nonEmptyFileCounter = 0; int emptyFileCounter = 0; for (int i = 0; i < files.length; i++) { files[i].setHasStream(isEmptyStream == null ? true : !isEmptyStream.get(i)); if (files[i].hasStream()) { files[i].setDirectory(false); files[i].setAntiItem(false); files[i].setHasCrc(archive.subStreamsInfo.hasCrc.get(nonEmptyFileCounter)); files[i].setCrcValue(archive.subStreamsInfo.crcs[nonEmptyFileCounter]); files[i].setSize(archive.subStreamsInfo.unpackSizes[nonEmptyFileCounter]); ++nonEmptyFileCounter; } else { files[i].setDirectory(isEmptyFile == null ? true : !isEmptyFile.get(emptyFileCounter)); files[i].setAntiItem(isAnti == null ? false : isAnti.get(emptyFileCounter)); files[i].setHasCrc(false); files[i].setSize(0); ++emptyFileCounter; } } archive.files = files; calculateStreamMap(archive); }