List of usage examples for java.nio ByteBuffer rewind
public final Buffer rewind()
From source file:ffx.xray.parsers.MTZWriter.java
/** * <p>/*from w ww . java 2s .co m*/ * write</p> */ public void write() { ByteOrder byteOrder = ByteOrder.nativeOrder(); FileOutputStream fileOutputStream; DataOutputStream dataOutputStream; try { if (logger.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(); sb.append(format("\n Writing MTZ HKL file: \"%s\"\n", fileName)); logger.info(sb.toString()); } fileOutputStream = new FileOutputStream(fileName); dataOutputStream = new DataOutputStream(fileOutputStream); byte bytes[] = new byte[80]; int offset = 0; int writeLen = 0; int iMapData; float fMapData; // Header. StringBuilder sb = new StringBuilder(); sb.append("MTZ "); dataOutputStream.writeBytes(sb.toString()); // Header offset. int headerOffset = n * nCol + 21; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.order(byteOrder).putInt(headerOffset); // Machine code: double, float, int, uchar // 0x4441 for LE, 0x1111 for BE if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { iMapData = 0x4441; } else { iMapData = 0x1111; } byteBuffer.order(byteOrder).putInt(iMapData); dataOutputStream.write(bytes, offset, 8); sb = new StringBuilder(); sb.append(" "); sb.setLength(68); dataOutputStream.writeBytes(sb.toString()); // Data. Vector<String> colname = new Vector<>(nCol); char colType[] = new char[nCol]; double res[] = new double[2]; res[0] = Double.POSITIVE_INFINITY; res[1] = Double.NEGATIVE_INFINITY; float colMinMax[][] = new float[nCol][2]; for (int i = 0; i < nCol; i++) { colMinMax[i][0] = Float.POSITIVE_INFINITY; colMinMax[i][1] = Float.NEGATIVE_INFINITY; } ReflectionSpline sigmaASpline = new ReflectionSpline(reflectionList, refinementData.sigmaa.length); int col = 0; colname.add("H"); colType[col++] = 'H'; colname.add("K"); colType[col++] = 'H'; colname.add("L"); colType[col++] = 'H'; writeLen += 12; if (mtzType != MTZType.FCONLY) { colname.add("FO"); colType[col++] = 'F'; colname.add("SIGFO"); colType[col++] = 'Q'; colname.add("FreeR"); colType[col++] = 'I'; writeLen += 12; } if (mtzType != MTZType.DATAONLY) { colname.add("Fs"); colType[col++] = 'F'; colname.add("PHIFs"); colType[col++] = 'P'; colname.add("Fc"); colType[col++] = 'F'; colname.add("PHIFc"); colType[col++] = 'P'; writeLen += 16; } if (mtzType == MTZType.ALL) { colname.add("FOM"); colType[col++] = 'W'; colname.add("PHIW"); colType[col++] = 'P'; colname.add("SigmaAs"); colType[col++] = 'F'; colname.add("SigmaAw"); colType[col++] = 'Q'; colname.add("FWT"); colType[col++] = 'F'; colname.add("PHWT"); colType[col++] = 'P'; colname.add("DELFWT"); colType[col++] = 'F'; colname.add("PHDELWT"); colType[col++] = 'P'; writeLen += 32; } for (HKL ih : reflectionList.hkllist) { col = 0; int i = ih.index(); // Skip the 0 0 0 reflection. if (ih.h() == 0 && ih.k() == 0 && ih.l() == 0) { continue; } double ss = Crystal.invressq(crystal, ih); res[0] = min(ss, res[0]); res[1] = max(ss, res[1]); // HKL first (3) fMapData = ih.h(); colMinMax[col][0] = min(fMapData, colMinMax[0][0]); colMinMax[col][1] = max(fMapData, colMinMax[0][1]); byteBuffer.rewind(); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = ih.k(); colMinMax[col][0] = min(fMapData, colMinMax[1][0]); colMinMax[col][1] = max(fMapData, colMinMax[1][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = ih.l(); colMinMax[col][0] = min(fMapData, colMinMax[2][0]); colMinMax[col][1] = max(fMapData, colMinMax[2][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; if (mtzType != MTZType.FCONLY) { // F/sigF (2) fMapData = (float) refinementData.getF(i); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) refinementData.getSigF(i); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; // Free R (1) fMapData = (float) refinementData.getFreeR(i); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; } if (mtzType == MTZType.FCONLY) { // Fs (2) fMapData = (float) refinementData.fsF(i); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.fsPhi(i)); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; // Fc (unscaled!) (2) fMapData = (float) refinementData.fcF(i); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) Math.toDegrees(refinementData.fcPhi(i)); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; } if (mtzType == MTZType.ALL) { // Fs (2) fMapData = (float) refinementData.fsF(i); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.fsPhi(i)); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; // Fctot (2) fMapData = (float) refinementData.fcTotF(i); if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.fcTotPhi(i)); if (!isNaN(fMapData)) { colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]); colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; // FOM/phase (2) fMapData = (float) refinementData.fomphi[i][0]; if (!isNaN(fMapData)) { colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]); colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.fomphi[i][1]); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; // Spline setup. double fh = spline.f(ss, refinementData.spline); double sa = sigmaASpline.f(ss, refinementData.sigmaa); double wa = sigmaASpline.f(ss, refinementData.sigmaw); // sigmaA/w (2) fMapData = (float) sa; if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) wa; if (!isNaN(fMapData)) { colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); } byteBuffer.order(byteOrder).putFloat(fMapData); col++; // Map coeffs (4). fMapData = (float) refinementData.FoFc2F(i); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.FoFc2Phi(i)); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) refinementData.foFc1F(i); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; fMapData = (float) toDegrees(refinementData.foFc1Phi(i)); colMinMax[col][0] = min(fMapData, colMinMax[col][0]); colMinMax[col][1] = max(fMapData, colMinMax[col][1]); byteBuffer.order(byteOrder).putFloat(fMapData); col++; } dataOutputStream.write(bytes, offset, writeLen); } // Header. sb = new StringBuilder(); sb.append("VERS MTZ:V1.1 "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss "); sb = new StringBuilder(); sb.append("TITLE FFX output: " + sdf.format(now)); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append(String.format("NCOL %8d %12d %8d", nCol, n, 0)); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("SORT 0 0 0 0 0 "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); char cdata = spaceGroup.shortName.charAt(0); if (cdata == 'H') { cdata = 'R'; } sb.append(String.format("SYMINF %3d %2d %c %5d %22s %5s", spaceGroup.getNumberOfSymOps(), spaceGroup.numPrimitiveSymEquiv, cdata, spaceGroup.number, "'" + spaceGroup.shortName + "'", spaceGroup.pointGroupName)); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); for (int i = 0; i < spaceGroup.symOps.size(); i++) { sb = new StringBuilder(); sb.append("SYMM "); SymOp symop = spaceGroup.symOps.get(i); sb.append(symop.toXYZString()); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); } sb = new StringBuilder(); sb.append(String.format("RESO %8.6f%13s%8.6f", res[0], " ", res[1])); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("VALM NAN "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("NDIF 1 "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("PROJECT 1 project "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("CRYSTAL 1 crystal "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("DATASET 1 dataset "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); for (int j = 0; j < nCol; j++) { sb = new StringBuilder(); sb.append(String.format("COLUMN %-30s %c %17.4f %17.4f 1", colname.get(j), colType[j], colMinMax[j][0], colMinMax[j][1])); dataOutputStream.writeBytes(sb.toString()); } sb = new StringBuilder(); sb.append(String.format("CELL %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", crystal.a, crystal.b, crystal.c, crystal.alpha, crystal.beta, crystal.gamma)); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append(String.format("DCELL %9d %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", 1, crystal.a, crystal.b, crystal.c, crystal.alpha, crystal.beta, crystal.gamma)); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("DWAVEL 1 1.00000 "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("END "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); sb = new StringBuilder(); sb.append("MTZENDOFHEADERS "); while (sb.length() < 80) { sb.append(" "); } dataOutputStream.writeBytes(sb.toString()); dataOutputStream.close(); } catch (Exception e) { String message = "Fatal exception evaluating structure factors.\n"; logger.log(Level.SEVERE, message, e); } }
From source file:com.act.lcms.v2.fullindex.Builder.java
protected void extractTriples(Iterator<LCMSSpectrum> iter, List<MZWindow> windows) throws RocksDBException, IOException { /* Warning: this method makes heavy use of ByteBuffers to perform memory efficient collection of values and * conversion of those values into byte arrays that RocksDB can consume. If you haven't already, go read this * tutorial on ByteBuffers: http://mindprod.com/jgloss/bytebuffer.html */*from w w w. j a v a2 s.c o m*/ * ByteBuffers are quite low-level structures, and they use some terms you need to watch out for: * capacity: The total number of bytes in the array backing the buffer. Don't write more than this. * position: The next index in the buffer to read or write a byte. Moves with each read or write op. * limit: A mark of where the final byte in the buffer was written. Don't read past this. * The remaining() call is affected by the limit. * mark: Ignore this for now, we don't use it. (We'll always, always read buffers from 0.) * * And here are some methods that we'll use often: * clear: Set position = 0, limit = 0. Pretend the buffer is empty, and is ready for more writes. * flip: Set limit = position, then position = 0. This remembers how many bytes were written to the buffer * (as the current position), and then puts the position at the beginning. * Always call this after the write before a read. * rewind: Set position = 0. Buffer is ready for reading, but unless the limit was set we might now know how * many bytes there are to read. Always call flip() before rewind(). Can rewind many times to re-read * the buffer repeatedly. * remaining: How many bytes do we have left to read? Requires an accurate limit value to avoid garbage bytes. * reset: Don't use this. It uses the mark, which we don't need currently. * * Write/read patterns look like: * buffer.clear(); // Clear out anything already in the buffer. * buffer.put(thing1).put(thing2)... // write a bunch of stuff * buffer.flip(); // Prep for reading. Call *once*! * * while (buffer.hasRemaining()) { buffer.get(); } // Read a bunch of stuff. * buffer.rewind(); // Ready for reading again! * while (buffer.hasRemaining()) { buffer.get(); } // Etc. * buffer.reset(); // Forget what was written previously, buffer is ready for reuse. * * We use byte buffers because they're fast, efficient, and offer incredibly convenient means of serializing a * stream of primitive types to their minimal binary representations. The same operations on objects + object * streams require significantly more CPU cycles, consume more memory, and tend to be brittle (i.e. if a class * definition changes slightly, serialization may break). Since the data we're dealing with is pretty simple, we * opt for the low-level approach. */ /* Because we'll eventually use the window indices to map a mz range to a list of triples that fall within that * range, verify that all of the indices are unique. If they're not, we'll end up overwriting the data in and * corrupting the structure of the index. */ ensureUniqueMZWindowIndices(windows); // For every mz window, allocate a buffer to hold the indices of the triples that fall in that window. ByteBuffer[] mzWindowTripleBuffers = new ByteBuffer[windows.size()]; for (int i = 0; i < mzWindowTripleBuffers.length; i++) { /* Note: the mapping between these buffers and their respective mzWindows is purely positional. Specifically, * mzWindows.get(i).getIndex() != i, but mzWindowTripleBuffers[i] belongs to mzWindows.get(i). We'll map windows * indices to the contents of mzWindowTripleBuffers at the very end of this function. */ mzWindowTripleBuffers[i] = ByteBuffer.allocate(Long.BYTES * 4096); // Start with 4096 longs = 8 pages per window. } // Every TMzI gets an index which we'll use later when we're querying by m/z and time. long counter = -1; // We increment at the top of the loop. // Note: we could also write to an mmapped file and just track pointers, but then we might lose out on compression. // We allocate all the buffers strictly here, as we know how many bytes a long and a triple will take. Then reuse! ByteBuffer counterBuffer = ByteBuffer.allocate(Long.BYTES); ByteBuffer valBuffer = ByteBuffer.allocate(TMzI.BYTES); List<Float> timepoints = new ArrayList<>(2000); // We can be sloppy here, as the count is small. /* We use a sweep-line approach to scanning through the m/z windows so that we can aggregate all intensities in * one pass over the current LCMSSpectrum (this saves us one inner loop in our extraction process). The m/z * values in the LCMSSpectrum become our "critical" or "interesting points" over which we sweep our m/z ranges. * The next window in m/z order is guaranteed to be the next one we want to consider since we address the points * in m/z order as well. As soon as we've passed out of the range of one of our windows, we discard it. It is * valid for a window to be added to and discarded from the working queue in one application of the work loop. */ LinkedList<MZWindow> tbdQueueTemplate = new LinkedList<>(windows); // We can reuse this template to init the sweep. int spectrumCounter = 0; while (iter.hasNext()) { LCMSSpectrum spectrum = iter.next(); float time = spectrum.getTimeVal().floatValue(); // This will record all the m/z + intensity readings that correspond to this timepoint. Exactly sized too! ByteBuffer triplesForThisTime = ByteBuffer.allocate(Long.BYTES * spectrum.getIntensities().size()); // Batch up all the triple writes to reduce the number of times we hit the disk in this loop. // Note: huge success! RocksDBAndHandles.RocksDBWriteBatch<ColumnFamilies> writeBatch = dbAndHandles.makeWriteBatch(); // Initialize the sweep line lists. Windows go follow: tbd -> working -> done (nowhere). LinkedList<MZWindow> workingQueue = new LinkedList<>(); LinkedList<MZWindow> tbdQueue = (LinkedList<MZWindow>) tbdQueueTemplate.clone(); // clone is in the docs, so okay! for (Pair<Double, Double> mzIntensity : spectrum.getIntensities()) { // Very important: increment the counter for every triple. Otherwise we'll overwrite triples = Very Bad (tm). counter++; // Brevity = soul of wit! Double mz = mzIntensity.getLeft(); Double intensity = mzIntensity.getRight(); // Reset the buffers so we end up re-using the few bytes we've allocated. counterBuffer.clear(); // Empty (virtually). counterBuffer.putLong(counter); counterBuffer.flip(); // Prep for reading. valBuffer.clear(); // Empty (virtually). TMzI.writeToByteBuffer(valBuffer, time, mz, intensity.floatValue()); valBuffer.flip(); // Prep for reading. // First, shift any applicable ranges onto the working queue based on their minimum mz. while (!tbdQueue.isEmpty() && tbdQueue.peekFirst().getMin() <= mz) { workingQueue.add(tbdQueue.pop()); } // Next, remove any ranges we've passed. while (!workingQueue.isEmpty() && workingQueue.peekFirst().getMax() < mz) { workingQueue.pop(); // TODO: add() this to a recovery queue which can then become the tbdQueue. Edge cases! } /* In the old indexed trace extractor world, we could bail here if there were no target m/z's in our window set * that matched with the m/z of our current mzIntensity. However, since we're now also recording the links * between timepoints and their (t, m/z, i) triples, we need to keep on keepin' on regardless of whether we have * any m/z windows in the working set right now. */ // The working queue should now hold only ranges that include this m/z value. Sweep line swept! /* Now add this intensity to the buffers of all the windows in the working queue. Note that since we're only * storing the *index* of the triple, these buffers are going to consume less space than they would if we * stored everything together. */ for (MZWindow window : workingQueue) { // TODO: count the number of times we add intensities to each window's accumulator for MS1-style warnings. counterBuffer.rewind(); // Already flipped. mzWindowTripleBuffers[window.getIndex()] = // Must assign when calling appendOrRealloc. Utils.appendOrRealloc(mzWindowTripleBuffers[window.getIndex()], counterBuffer); } // We flipped after reading, so we should be good to rewind (to be safe) and write here. counterBuffer.rewind(); valBuffer.rewind(); writeBatch.put(ColumnFamilies.ID_TO_TRIPLE, Utils.toCompactArray(counterBuffer), Utils.toCompactArray(valBuffer)); // Rewind again for another read. counterBuffer.rewind(); triplesForThisTime.put(counterBuffer); } writeBatch.write(); assert (triplesForThisTime.position() == triplesForThisTime.capacity()); ByteBuffer timeBuffer = ByteBuffer.allocate(Float.BYTES).putFloat(time); timeBuffer.flip(); // Prep both bufers for reading so they can be written to the DB. triplesForThisTime.flip(); dbAndHandles.put(ColumnFamilies.TIMEPOINT_TO_TRIPLES, Utils.toCompactArray(timeBuffer), Utils.toCompactArray(triplesForThisTime)); timepoints.add(time); spectrumCounter++; if (spectrumCounter % 1000 == 0) { LOGGER.info("Extracted %d time spectra", spectrumCounter); } } LOGGER.info("Extracted %d total time spectra", spectrumCounter); // Now write all the mzWindow to triple indexes. RocksDBAndHandles.RocksDBWriteBatch<ColumnFamilies> writeBatch = dbAndHandles.makeWriteBatch(); ByteBuffer idBuffer = ByteBuffer.allocate(Integer.BYTES); for (int i = 0; i < mzWindowTripleBuffers.length; i++) { idBuffer.clear(); idBuffer.putInt(windows.get(i).getIndex()); idBuffer.flip(); ByteBuffer triplesBuffer = mzWindowTripleBuffers[i]; triplesBuffer.flip(); // Prep for read. writeBatch.put(ColumnFamilies.WINDOW_ID_TO_TRIPLES, Utils.toCompactArray(idBuffer), Utils.toCompactArray(triplesBuffer)); } writeBatch.write(); dbAndHandles.put(ColumnFamilies.TIMEPOINTS, TIMEPOINTS_KEY, Utils.floatListToByteArray(timepoints)); dbAndHandles.flush(true); }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
private void writeStatusToFile(FileChannel channel) throws IOException { long size = getBufferSize(); ByteBuffer buffer; if (useNIOMemoryMapping) { MappedByteBuffer mbb = channel.map(MapMode.READ_WRITE, 0, size); mbb.load();//from w ww . j a va 2 s .com buffer = mbb; } else { channel.truncate(size); buffer = ByteBuffer.wrap(new byte[(int) size]); } buffer.position(0); buffer.putLong(version); CRC32 crc32 = new CRC32(); crc32.update((int) (version >>> 32) & 0xFFFFFFFF); crc32.update((int) (version >>> 0) & 0xFFFFFFFF); buffer.putInt(indexEntries.size()); crc32.update(indexEntries.size()); for (IndexEntry entry : indexEntries.values()) { String entryType = entry.getType().toString(); writeString(buffer, crc32, entryType); writeString(buffer, crc32, entry.getName()); writeString(buffer, crc32, entry.getParentName()); String entryStatus = entry.getStatus().toString(); writeString(buffer, crc32, entryStatus); writeString(buffer, crc32, entry.getMergeId()); buffer.putLong(entry.getDocumentCount()); crc32.update((int) (entry.getDocumentCount() >>> 32) & 0xFFFFFFFF); crc32.update((int) (entry.getDocumentCount() >>> 0) & 0xFFFFFFFF); buffer.putLong(entry.getDeletions()); crc32.update((int) (entry.getDeletions() >>> 32) & 0xFFFFFFFF); crc32.update((int) (entry.getDeletions() >>> 0) & 0xFFFFFFFF); buffer.put(entry.isDeletOnlyNodes() ? (byte) 1 : (byte) 0); crc32.update(entry.isDeletOnlyNodes() ? new byte[] { (byte) 1 } : new byte[] { (byte) 0 }); } buffer.putLong(crc32.getValue()); if (useNIOMemoryMapping) { ((MappedByteBuffer) buffer).force(); } else { buffer.rewind(); channel.position(0); channel.write(buffer); } }
From source file:ffx.realspace.CCP4MapFilter.java
/** * {@inheritDoc}// w w w . ja va 2 s .co m */ @Override public boolean readFile(String filename, RealSpaceRefinementData refinementdata, CompositeConfiguration properties) { int imapData; double cellA, cellB, cellC, cellAlpha, cellBeta, cellGamma; String stampString; ByteOrder byteOrder = ByteOrder.nativeOrder(); FileInputStream fileInputStream; DataInputStream dataInputStream; double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; double mean = 0.0; double sd = 0.0; double rmsd = 0.0; // First determine byte order of file versus system try { fileInputStream = new FileInputStream(filename); dataInputStream = new DataInputStream(fileInputStream); dataInputStream.skipBytes(212); byte bytes[] = new byte[4]; dataInputStream.read(bytes, 0, 4); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); imapData = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt(); stampString = Integer.toHexString(imapData); switch (stampString.charAt(0)) { case '1': case '3': if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) { byteOrder = ByteOrder.BIG_ENDIAN; } break; case '4': if (byteOrder.equals(ByteOrder.BIG_ENDIAN)) { byteOrder = ByteOrder.LITTLE_ENDIAN; } break; } if (logger.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(); sb.append(String.format("\n Opening CCP4 map: %s\n", filename)); //sb.append(String.format("file type (machine stamp): %s\n", stampString)); logger.info(sb.toString()); } fileInputStream.close(); } catch (Exception e) { String message = " Fatal exception reading CCP4 map.\n"; logger.log(Level.SEVERE, message, e); } try { fileInputStream = new FileInputStream(filename); dataInputStream = new DataInputStream(fileInputStream); byte bytes[] = new byte[2048]; dataInputStream.read(bytes, 0, 1024); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int ext[] = new int[3]; ext[0] = byteBuffer.order(byteOrder).getInt(); ext[1] = byteBuffer.order(byteOrder).getInt(); ext[2] = byteBuffer.order(byteOrder).getInt(); // mode (2 = reals, only one we accept) int mode = byteBuffer.order(byteOrder).getInt(); int ori[] = new int[3]; ori[0] = byteBuffer.order(byteOrder).getInt(); ori[1] = byteBuffer.order(byteOrder).getInt(); ori[2] = byteBuffer.order(byteOrder).getInt(); int ni[] = new int[3]; ni[0] = byteBuffer.order(byteOrder).getInt(); ni[1] = byteBuffer.order(byteOrder).getInt(); ni[2] = byteBuffer.order(byteOrder).getInt(); cellA = byteBuffer.order(byteOrder).getFloat(); cellB = byteBuffer.order(byteOrder).getFloat(); cellC = byteBuffer.order(byteOrder).getFloat(); cellAlpha = byteBuffer.order(byteOrder).getFloat(); cellBeta = byteBuffer.order(byteOrder).getFloat(); cellGamma = byteBuffer.order(byteOrder).getFloat(); int axisi[] = new int[3]; for (int i = 0; i < 3; i++) { int axis = byteBuffer.order(byteOrder).getInt(); switch (axis) { case 1: axisi[0] = i; break; case 2: axisi[1] = i; break; case 3: axisi[2] = i; break; } } min = byteBuffer.order(byteOrder).getFloat(); max = byteBuffer.order(byteOrder).getFloat(); mean = byteBuffer.order(byteOrder).getFloat(); int sg = byteBuffer.order(byteOrder).getInt(); int nsymb = byteBuffer.order(byteOrder).getInt(); int skew = byteBuffer.order(byteOrder).getInt(); for (int i = 0; i < 12; i++) { byteBuffer.order(byteOrder).getFloat(); } for (int i = 0; i < 15; i++) { byteBuffer.order(byteOrder).getInt(); } byte word[] = new byte[2048]; byteBuffer.order(byteOrder).get(word, 0, 4); String mapString = new String(word); sd = byteBuffer.order(byteOrder).getFloat(); rmsd = byteBuffer.order(byteOrder).getFloat(); if (logger.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(); sb.append(String.format(" Column origin: %d\t Extent: %d\n", ori[0], ext[0])); sb.append(String.format(" Row origin: %d\t Extent: %d\n", ori[1], ext[1])); sb.append(String.format(" Section origin: %d\t Extent: %d\n", ori[2], ext[2])); sb.append(String.format(" Axis order: %d %d %d\n", axisi[0], axisi[1], axisi[2])); sb.append(String.format(" Number of X, Y, Z columns: %d %d %d\n", ni[0], ni[1], ni[2])); sb.append(String.format(" Spacegroup: %d (%s)\n", sg, SpaceGroup.spaceGroupNames[sg - 1])); sb.append(String.format(" Cell: %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f\n", cellA, cellB, cellC, cellAlpha, cellBeta, cellGamma)); logger.info(sb.toString()); } int nlabel = byteBuffer.order(byteOrder).getInt(); for (int i = 0; i < 10; i++) { byteBuffer.order(byteOrder).get(word, 0, 80); mapString = new String(word); } if (nsymb > 0) { byteBuffer.rewind(); dataInputStream.read(bytes, 0, nsymb); for (int i = 0; i < nsymb / 80; i += 80) { byteBuffer.order(byteOrder).get(word, 0, 80); mapString = new String(word); } } byteBuffer.rewind(); dataInputStream.read(bytes, 0, 2048); refinementdata.setData(new double[ext[0] * ext[1] * ext[2]]); int ijk[] = new int[3]; int index, x, y, z; refinementdata.setOrigin(ori[axisi[0]], ori[axisi[1]], ori[axisi[2]]); int nx = ext[axisi[0]]; int ny = ext[axisi[1]]; int nz = ext[axisi[2]]; refinementdata.setExtent(nx, ny, nz); refinementdata.setNI(ni[0], ni[1], ni[2]); for (ijk[2] = 0; ijk[2] < ext[2]; ijk[2]++) { for (ijk[1] = 0; ijk[1] < ext[1]; ijk[1]++) { for (ijk[0] = 0; ijk[0] < ext[0]; ijk[0]++) { x = ijk[axisi[0]]; y = ijk[axisi[1]]; z = ijk[axisi[2]]; index = x + nx * (y + ny * z); refinementdata.getData()[index] = byteBuffer.order(byteOrder).getFloat(); if (!byteBuffer.hasRemaining()) { byteBuffer.rewind(); dataInputStream.read(bytes, 0, 2048); } } } } fileInputStream.close(); } catch (Exception e) { String message = " Fatal exception reading CCP4 map.\n"; logger.log(Level.SEVERE, message, e); } return true; }
From source file:org.apache.hadoop.hbase.coprocessor.transactional.TrxRegionEndpoint.java
/** * Gives the row count for the given column family and column qualifier, in * the given row range as defined in the Scan object. * @throws IOException/* w w w.j a v a2 s.com*/ */ @Override public void getRowNum(RpcController controller, TransactionalAggregateRequest request, RpcCallback<TransactionalAggregateResponse> done) { TransactionalAggregateResponse response = null; long counter = 0L; List<Cell> results = new ArrayList<Cell>(); RegionScanner scanner = null; long transactionId = 0L; try { Scan scan = ProtobufUtil.toScan(request.getScan()); byte[][] colFamilies = scan.getFamilies(); byte[] colFamily = colFamilies != null ? colFamilies[0] : null; NavigableSet<byte[]> qualifiers = colFamilies != null ? scan.getFamilyMap().get(colFamily) : null; byte[] qualifier = null; if (qualifiers != null && !qualifiers.isEmpty()) { qualifier = qualifiers.pollFirst(); } if (scan.getFilter() == null && qualifier == null) scan.setFilter(new FirstKeyOnlyFilter()); transactionId = request.getTransactionId(); scanner = getScanner(transactionId, scan); boolean hasMoreRows = false; do { hasMoreRows = scanner.next(results); if (results.size() > 0) { counter++; } results.clear(); } while (hasMoreRows); ByteBuffer bb = ByteBuffer.allocate(8).putLong(counter); bb.rewind(); response = TransactionalAggregateResponse.newBuilder().addFirstPart(ByteString.copyFrom(bb)).build(); } catch (IOException e) { ResponseConverter.setControllerException(controller, e); } finally { if (scanner != null) { try { scanner.close(); } catch (IOException ignored) { } } } if (LOG.isInfoEnabled()) LOG.info(String.format( "Row counter for txId %d from this region: %s is %d, startKey is [%s], endKey is [%s]", transactionId, env.getRegion().getRegionNameAsString(), counter, env.getRegion().getStartKey() == null ? "null" : Bytes.toStringBinary(env.getRegion().getStartKey()), env.getRegion().getEndKey() == null ? "null" : Bytes.toStringBinary(env.getRegion().getEndKey()))); done.run(response); }
From source file:org.apache.hadoop.hbase.coprocessor.transactional.TrxRegionEndpoint.java
/** * Gives a Pair with first object as Sum and second object as row count, * computed for a given combination of column qualifier and column family in * the given row range as defined in the Scan object. In its current * implementation, it takes one column family and one column qualifier (if * provided). In case of null column qualifier, an aggregate sum over all the * entire column family will be returned. * <p>/* ww w . j a v a 2 s .co m*/ * The average is computed in * AggregationClient#avg(byte[], ColumnInterpreter, Scan) by * processing results from all regions, so its "ok" to pass sum and a Long * type. */ @Override public void getAvg(RpcController controller, TransactionalAggregateRequest request, RpcCallback<TransactionalAggregateResponse> done) { TransactionalAggregateResponse response = null; RegionScanner scanner = null; try { ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request); S sumVal = null; Long rowCountVal = 0l; Scan scan = ProtobufUtil.toScan(request.getScan()); long transactionId = request.getTransactionId(); scanner = getScanner(transactionId, scan); byte[] colFamily = scan.getFamilies()[0]; NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily); byte[] qualifier = null; if (qualifiers != null && !qualifiers.isEmpty()) { qualifier = qualifiers.pollFirst(); } List<Cell> results = new ArrayList<Cell>(); boolean hasMoreRows = false; do { results.clear(); hasMoreRows = scanner.next(results); for (Cell kv : results) { sumVal = ci.add(sumVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, kv))); } rowCountVal++; } while (hasMoreRows); if (sumVal != null) { ByteString first = ci.getProtoForPromotedType(sumVal).toByteString(); TransactionalAggregateResponse.Builder pair = TransactionalAggregateResponse.newBuilder(); pair.addFirstPart(first); ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal); bb.rewind(); pair.setSecondPart(ByteString.copyFrom(bb)); response = pair.build(); } } catch (IOException e) { ResponseConverter.setControllerException(controller, e); } finally { if (scanner != null) { try { scanner.close(); } catch (IOException ignored) { } } } done.run(response); }
From source file:org.apache.hadoop.hbase.coprocessor.transactional.TrxRegionEndpoint.java
/** * Gives a Pair with first object a List containing Sum and sum of squares, * and the second object as row count. It is computed for a given combination of * column qualifier and column family in the given row range as defined in the * Scan object. In its current implementation, it takes one column family and * one column qualifier (if provided). The idea is get the value of variance first: * the average of the squares less the square of the average a standard * deviation is square root of variance. */// w w w .j a v a 2 s . c o m @Override public void getStd(RpcController controller, TransactionalAggregateRequest request, RpcCallback<TransactionalAggregateResponse> done) { RegionScanner scanner = null; TransactionalAggregateResponse response = null; try { ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request); S sumVal = null, sumSqVal = null, tempVal = null; long rowCountVal = 0l; Scan scan = ProtobufUtil.toScan(request.getScan()); long transactionId = request.getTransactionId(); scanner = getScanner(transactionId, scan); byte[] colFamily = scan.getFamilies()[0]; NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily); byte[] qualifier = null; if (qualifiers != null && !qualifiers.isEmpty()) { qualifier = qualifiers.pollFirst(); } List<Cell> results = new ArrayList<Cell>(); boolean hasMoreRows = false; do { tempVal = null; hasMoreRows = scanner.next(results); for (Cell kv : results) { tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, kv))); } results.clear(); sumVal = ci.add(sumVal, tempVal); sumSqVal = ci.add(sumSqVal, ci.multiply(tempVal, tempVal)); rowCountVal++; } while (hasMoreRows); if (sumVal != null) { ByteString first_sumVal = ci.getProtoForPromotedType(sumVal).toByteString(); ByteString first_sumSqVal = ci.getProtoForPromotedType(sumSqVal).toByteString(); TransactionalAggregateResponse.Builder pair = TransactionalAggregateResponse.newBuilder(); pair.addFirstPart(first_sumVal); pair.addFirstPart(first_sumSqVal); ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal); bb.rewind(); pair.setSecondPart(ByteString.copyFrom(bb)); response = pair.build(); } } catch (IOException e) { ResponseConverter.setControllerException(controller, e); } finally { if (scanner != null) { try { scanner.close(); } catch (IOException ignored) { } } } done.run(response); }
From source file:ffx.xray.CCP4MapFilter.java
/** * {@inheritDoc}//from w ww. j av a 2 s .c o m */ @Override public boolean readFile(String filename, RealSpaceRefinementData refinementdata, CompositeConfiguration properties) { int imapdata; double cella, cellb, cellc, cellalpha, cellbeta, cellgamma; String stampstr; ByteOrder b = ByteOrder.nativeOrder(); FileInputStream fis; DataInputStream dis; double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; double mean = 0.0; double sd = 0.0; double rmsd = 0.0; // first determine byte order of file versus system try { fis = new FileInputStream(filename); dis = new DataInputStream(fis); dis.skipBytes(212); byte bytes[] = new byte[4]; dis.read(bytes, 0, 4); ByteBuffer bb = ByteBuffer.wrap(bytes); imapdata = bb.order(ByteOrder.BIG_ENDIAN).getInt(); stampstr = Integer.toHexString(imapdata); // System.out.println("stamp: " + stampstr); switch (stampstr.charAt(0)) { case '1': case '3': if (b.equals(ByteOrder.LITTLE_ENDIAN)) { b = ByteOrder.BIG_ENDIAN; } break; case '4': if (b.equals(ByteOrder.BIG_ENDIAN)) { b = ByteOrder.LITTLE_ENDIAN; } break; } if (logger.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(); sb.append(String.format("\nOpening CCP4 map: %s\n", filename)); sb.append(String.format("file type (machine stamp): %s\n", stampstr)); logger.info(sb.toString()); } fis.close(); } catch (Exception e) { String message = "Fatal exception reading CCP4 map.\n"; logger.log(Level.SEVERE, message, e); System.exit(-1); } try { fis = new FileInputStream(filename); dis = new DataInputStream(fis); byte bytes[] = new byte[2048]; dis.read(bytes, 0, 1024); ByteBuffer bb = ByteBuffer.wrap(bytes); int ext[] = new int[3]; ext[0] = bb.order(b).getInt(); ext[1] = bb.order(b).getInt(); ext[2] = bb.order(b).getInt(); // mode (2 = reals, only one we accept) int mode = bb.order(b).getInt(); int ori[] = new int[3]; ori[0] = bb.order(b).getInt(); ori[1] = bb.order(b).getInt(); ori[2] = bb.order(b).getInt(); int ni[] = new int[3]; ni[0] = bb.order(b).getInt(); ni[1] = bb.order(b).getInt(); ni[2] = bb.order(b).getInt(); cella = bb.order(b).getFloat(); cellb = bb.order(b).getFloat(); cellc = bb.order(b).getFloat(); cellalpha = bb.order(b).getFloat(); cellbeta = bb.order(b).getFloat(); cellgamma = bb.order(b).getFloat(); int axisi[] = new int[3]; for (int i = 0; i < 3; i++) { int axis = bb.order(b).getInt(); switch (axis) { case 1: axisi[0] = i; break; case 2: axisi[1] = i; break; case 3: axisi[2] = i; break; } } min = bb.order(b).getFloat(); max = bb.order(b).getFloat(); mean = bb.order(b).getFloat(); int sg = bb.order(b).getInt(); int nsymb = bb.order(b).getInt(); int skew = bb.order(b).getInt(); for (int i = 0; i < 12; i++) { bb.order(b).getFloat(); } for (int i = 0; i < 15; i++) { bb.order(b).getInt(); } byte word[] = new byte[2048]; bb.order(b).get(word, 0, 4); String mapstr = new String(word); // System.out.println("MAP?: " + mapstr); sd = bb.order(b).getFloat(); rmsd = bb.order(b).getFloat(); /* System.out.println("col: " + ori[0] + " " + ext[0] + " " + ni[0]); System.out.println("row: " + ori[1] + " " + ext[1] + " " + ni[1]); System.out.println("sec: " + ori[2] + " " + ext[2] + " " + ni[2]); System.out.println("order: " + axisi[0] + " " + axisi[1] + " " + axisi[2]); System.out.println("min: " + min + " max: " + max + " mean: " + mean); System.out.println("sd: " + sd + " rmsd: " + rmsd); System.out.println("sg: " + sg); System.out.println("a: " + cella + " b: " + cellb + " c: " + cellc + " alpha: " + cellalpha + " beta: " + cellbeta + " gamma: " + cellgamma); */ if (logger.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(); sb.append(String.format(" column origin: %d extent: %d\n", ori[0], ext[0])); sb.append(String.format(" row origin: %d extent: %d\n", ori[1], ext[1])); sb.append(String.format(" section origin: %d extent: %d\n", ori[2], ext[2])); sb.append(String.format(" axis order: %d %d %d\n", axisi[0], axisi[1], axisi[2])); sb.append(String.format(" number of X, Y, Z columns: %d %d %d\n", ni[0], ni[1], ni[2])); sb.append(String.format(" spacegroup #: %d (name: %s)\n", sg, SpaceGroup.spaceGroupNames[sg - 1])); sb.append(String.format(" cell: %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f\n", cella, cellb, cellc, cellalpha, cellbeta, cellgamma)); logger.info(sb.toString()); } int nlabel = bb.order(b).getInt(); // System.out.println("nsymb: " + nsymb + " nlabel: " + nlabel); for (int i = 0; i < 10; i++) { bb.order(b).get(word, 0, 80); mapstr = new String(word); // System.out.println("label " + i + " : " + mapstr); } if (nsymb > 0) { bb.rewind(); dis.read(bytes, 0, nsymb); for (int i = 0; i < nsymb / 80; i += 80) { bb.order(b).get(word, 0, 80); mapstr = new String(word); // System.out.println("symm: " + mapstr); } } bb.rewind(); dis.read(bytes, 0, 2048); refinementdata.data = new double[ext[0] * ext[1] * ext[2]]; int ijk[] = new int[3]; int index, x, y, z; refinementdata.ori[0] = ori[axisi[0]]; refinementdata.ori[1] = ori[axisi[1]]; refinementdata.ori[2] = ori[axisi[2]]; int nx = ext[axisi[0]]; int ny = ext[axisi[1]]; int nz = ext[axisi[2]]; refinementdata.ext[0] = nx; refinementdata.ext[1] = ny; refinementdata.ext[2] = nz; refinementdata.ni[0] = ni[0]; refinementdata.ni[1] = ni[1]; refinementdata.ni[2] = ni[2]; for (ijk[2] = 0; ijk[2] < ext[2]; ijk[2]++) { for (ijk[1] = 0; ijk[1] < ext[1]; ijk[1]++) { for (ijk[0] = 0; ijk[0] < ext[0]; ijk[0]++) { x = ijk[axisi[0]]; y = ijk[axisi[1]]; z = ijk[axisi[2]]; index = x + nx * (y + ny * z); refinementdata.data[index] = bb.order(b).getFloat(); if (!bb.hasRemaining()) { bb.rewind(); dis.read(bytes, 0, 2048); } } } } fis.close(); } catch (Exception e) { String message = "Fatal exception reading CCP4 map.\n"; logger.log(Level.SEVERE, message, e); System.exit(-1); } return true; }
From source file:com.web.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;/*from w w w.ja v a2 s . co m*/ ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //System.out.println(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //System.out.println("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //System.out.println("In read obect"); object = ois.readObject(); //System.out.println("Class Cast"); //System.out.println("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //System.out.println("readObject"); //System.out.println("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //System.out.println("classLoaderMap" // + urlClassLoaderMap); //System.out.println(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //System.out.println(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap .get(earServicesDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(jarservicesDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // System.out.println("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // System.out.println("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * System.out.println( * "current context classloader" + * classLoader); */ //System.out.println("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //System.out.println("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //System.out.println("bytesRead<length"); iterator.remove(); continue; } //System.out.println(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //System.out.println("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //System.out.println("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket("0.0.0.0", Integer.parseInt(nodesport[random.nextInt(nodesport.length)])); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //System.out.println("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // System.out.println("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //System.out.println("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //System.out.println("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //System.out.println(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //System.out.println("In write1"); numberOfServicesRequests++; //System.out.println("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } }
From source file:com.app.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;/*from w w w. j a v a2 s.c om*/ ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //log.info(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //log.info("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //log.info("In read obect"); object = ois.readObject(); //log.info("Class Cast"); //log.info("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //log.info("readObject"); //log.info("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //log.info("classLoaderMap" // + urlClassLoaderMap); //log.info(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //log.info(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap.get(deployDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // log.info("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // log.info("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * log.info( * "current context classloader" + * classLoader); */ //log.info("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //log.info("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //log.info("bytesRead<length"); iterator.remove(); continue; } //log.info(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //log.info("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //log.info("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; Enumeration<NodeResourceInfo> noderesourceInfos = addressmap.elements(); NodeResourceInfo noderesourceinfo = null; String ip = ""; int port = 1000; long memavailable = 0; long memcurr = 0; if (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); ; } } while (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { memcurr = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); if (memavailable <= memcurr) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = memcurr; } } } ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket(ip, port); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //log.info("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // log.info("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //log.info("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //log.info("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //log.info(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //log.info("In write1"); numberOfServicesRequests++; //log.info("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); } }