List of usage examples for java.io DataOutputStream writeInt
public final void writeInt(int v) throws IOException
int
to the underlying output stream as four bytes, high byte first. From source file:org.squidy.nodes.ScreenDispatcher.java
/** * @param data/*from ww w . ja v a 2 s. c o m*/ * @return * @throws ProcessException */ public IData process2(IData data) throws ProcessException { // TODO [RR]: This may causes an exception if squidy will be stopped but dispatching thread is still alive. if (captureScreen == null) { throw new ProcessException("Capture screen to capture a screenshot is null", data); } BufferedImage image = captureScreen.capture(); // Scale screen if set. if (scaleScreen) { image = scaleImageTo(image, remoteScreenWidth, remoteScreenHeight); } if (dispatchingDebugWindow != null) { screenCaptureDebugging.setImage(image); dispatchingDebugWindow.repaint(); } // Reopen dispatcher socket if connection has been closed. if (socket == null || socket.isClosed() || !socket.isConnected()) { if (LOG.isDebugEnabled()) { LOG.debug("Connection is closed. Trying to reconnect in 5000 ms."); } try { Thread.sleep(5000); } catch (InterruptedException e1) { throw new ProcessException(e1.getMessage(), e1); } reopenDispatcherSocket(); } else { try { float tmpScreenCaptureQuality = screenCaptureQuality / (float) 100; // System.out.println("QUAL: " + tmpScreenCaptureQuality); byte[] picture = ImageKit.getBytes(image, tmpScreenCaptureQuality); DataOutputStream stream = new DataOutputStream(socket.getOutputStream()); // byte[] picture = pictureStream.toByteArray(); // int v = picture.length; // System.out.println("LENGTH: " + v); // System.out.println((v >>> 24) & 0xFF); // System.out.println((v >>> 16) & 0xFF); // System.out.println((v >>> 8) & 0xFF); // System.out.println((v >>> 0) & 0xFF); // System.out.println("SIZE: " + picture.length); stream.writeInt(picture.length); stream.write(picture); } catch (IOException e) { closeDispatcherSocket(); if (LOG.isErrorEnabled()) { LOG.error(e.getMessage(), e); } } } // else { // if (LOG.isErrorEnabled()) { // LOG.error("Dispatching socket is not open or have been closed already."); // } //// throw new ProcessException("Dispatching socket is not open or have been closed already.", data); // } return data; }
From source file:com.linkedin.pinot.core.segment.creator.impl.inv.OffHeapBitmapInvertedIndexCreator.java
@Override public void seal() throws IOException { FileOutputStream fos = null;/*w w w. java 2s .co m*/ FileInputStream fisOffsets = null; FileInputStream fisBitmaps = null; final DataOutputStream bitmapsOut; final DataOutputStream offsetsOut; String tempOffsetsFile = invertedIndexFile + ".offsets"; String tempBitmapsFile = invertedIndexFile + ".binary"; try { // build the posting list constructPostingLists(); // we need two separate streams, one to write the offsets and another to write the serialized // bitmap data. We need two because we dont the serialized length of each bitmap without // constructing. offsetsOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tempOffsetsFile))); bitmapsOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tempBitmapsFile))); // write out offsets of bitmaps. The information can be used to access a certain bitmap // directly. // Totally (invertedIndex.length+1) offsets will be written out; the last offset is used to // calculate the length of // the last bitmap, which might be needed when accessing bitmaps randomly. // If a bitmap's offset is k, then k bytes need to be skipped to reach the bitmap. int startOffset = 4 * (cardinality + 1); offsetsOut.writeInt(startOffset);// The first bitmap's offset MutableRoaringBitmap bitmap = new MutableRoaringBitmap(); for (int i = 0; i < cardinality; i++) { bitmap.clear(); int length = postingListLengths.get(i); for (int j = 0; j < length; j++) { int bufferOffset = postingListStartOffsets.get(i) + j; int value = postingListBuffer.get(bufferOffset); bitmap.add(value); } // serialize bitmap to bitmapsOut stream bitmap.serialize(bitmapsOut); startOffset += bitmap.serializedSizeInBytes(); // write offset offsetsOut.writeInt(startOffset); } offsetsOut.close(); bitmapsOut.close(); // merge the two files by simply writing offsets data first and then bitmap serialized data fos = new FileOutputStream(invertedIndexFile); fisOffsets = new FileInputStream(tempOffsetsFile); fisBitmaps = new FileInputStream(tempBitmapsFile); FileChannel channelOffsets = fisOffsets.getChannel(); channelOffsets.transferTo(0, channelOffsets.size(), fos.getChannel()); FileChannel channelBitmaps = fisBitmaps.getChannel(); channelBitmaps.transferTo(0, channelBitmaps.size(), fos.getChannel()); LOGGER.debug("persisted bitmap inverted index for column : " + spec.getName() + " in " + invertedIndexFile.getAbsolutePath()); } catch (Exception e) { LOGGER.error("Exception while creating bitmap index for column:" + spec.getName(), e); } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(fisOffsets); IOUtils.closeQuietly(fisOffsets); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(fos); // MMaputils handles the null checks for buffer MmapUtils.unloadByteBuffer(origValueBuffer); origValueBuffer = null; valueBuffer = null; if (origLengths != null) { MmapUtils.unloadByteBuffer(origLengths); origLengths = null; lengths = null; } MmapUtils.unloadByteBuffer(origPostingListBuffer); origPostingListBuffer = null; postingListBuffer = null; MmapUtils.unloadByteBuffer(origPostingListCurrentOffsets); origPostingListCurrentOffsets = null; postingListCurrentOffsets = null; MmapUtils.unloadByteBuffer(origPostingListLengths); origPostingListLengths = null; postingListLengths = null; MmapUtils.unloadByteBuffer(origPostingListStartOffsets); origPostingListStartOffsets = null; postingListStartOffsets = null; FileUtils.deleteQuietly(new File(tempOffsetsFile)); FileUtils.deleteQuietly(new File(tempBitmapsFile)); } }
From source file:org.grails.gsp.compiler.GroovyPageParser.java
public void writeLineNumbers(File filename) throws IOException { DataOutputStream dataOut = null; try {// w ww . ja v a 2s . c om dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); int lineNumbersCount = out.getCurrentLineNumber() - 1; int[] lineNumbers = out.getLineNumbers(); dataOut.writeInt(lineNumbersCount); for (int i = 0; i < lineNumbersCount; i++) { dataOut.writeInt(lineNumbers[i]); } } finally { SpringIOUtils.closeQuietly(dataOut); } }
From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore.java
private void addStoreOrUpdateOps(SafeTransaction trx, RMDelegationTokenIdentifier rmDTIdentifier, Long renewDate, boolean isUpdate) throws Exception { // store RM delegation token String nodeCreatePath = getNodePath(delegationTokensRootPath, DELEGATION_TOKEN_PREFIX + rmDTIdentifier.getSequenceNumber()); ByteArrayOutputStream seqOs = new ByteArrayOutputStream(); DataOutputStream seqOut = new DataOutputStream(seqOs); RMDelegationTokenIdentifierData identifierData = new RMDelegationTokenIdentifierData(rmDTIdentifier, renewDate);/*from w w w .j a v a2 s . c o m*/ try { if (LOG.isDebugEnabled()) { LOG.debug((isUpdate ? "Storing " : "Updating ") + "RMDelegationToken_" + rmDTIdentifier.getSequenceNumber()); } if (isUpdate) { trx.setData(nodeCreatePath, identifierData.toByteArray(), -1); } else { trx.create(nodeCreatePath, identifierData.toByteArray(), zkAcl, CreateMode.PERSISTENT); // Update Sequence number only while storing DT seqOut.writeInt(rmDTIdentifier.getSequenceNumber()); if (LOG.isDebugEnabled()) { LOG.debug((isUpdate ? "Storing " : "Updating ") + dtSequenceNumberPath + ". SequenceNumber: " + rmDTIdentifier.getSequenceNumber()); } trx.setData(dtSequenceNumberPath, seqOs.toByteArray(), -1); } } finally { seqOs.close(); } }
From source file:ClassFile.java
/** * Write the class out as a stream of bytes to the output * stream.//from ww w .ja v a 2s.c o m * * Generally you will read a class file, manipulate * it in some way, and then write it out again before passing * it to <TT>defineClass</TT> in some class loader. */ public void write(OutputStream out) throws IOException, Exception { DataOutputStream dos = new DataOutputStream(out); if (!isValidClass) { throw new Exception("ClassFile::write() - Invalid Class"); } dos.writeInt(magic); dos.writeShort(majorVersion); dos.writeShort(minorVersion); dos.writeShort(constantPool.length); for (int i = 1; i < constantPool.length; i++) { if (constantPool[i] != null) constantPool[i].write(dos, constantPool); } dos.writeShort(accessFlags); dos.writeShort(ConstantPoolInfo.indexOf(thisClass, constantPool)); dos.writeShort(ConstantPoolInfo.indexOf(superClass, constantPool)); if (interfaces == null) { dos.writeShort(0); } else { dos.writeShort(interfaces.length); for (int i = 0; i < interfaces.length; i++) { dos.writeShort(ConstantPoolInfo.indexOf(interfaces[i], constantPool)); } } if (fields == null) { dos.writeShort(0); } else { dos.writeShort(fields.length); for (int i = 0; i < fields.length; i++) { fields[i].write(dos, constantPool); } } if (methods == null) { dos.writeShort(0); } else { dos.writeShort(methods.length); for (int i = 0; i < methods.length; i++) { methods[i].write(dos, constantPool); } } if (attributes == null) { dos.writeShort(0); } else { dos.writeShort(attributes.length); for (int i = 0; i < attributes.length; i++) { attributes[i].write(dos, constantPool); } } }
From source file:org.codehaus.groovy.grails.web.pages.GroovyPageParser.java
public void writeLineNumbers(File filename) throws IOException { DataOutputStream dataOut = null; try {//from w w w.j a v a 2 s.c om dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); int lineNumbersCount = out.getCurrentLineNumber() - 1; int[] lineNumbers = out.getLineNumbers(); dataOut.writeInt(lineNumbersCount); for (int i = 0; i < lineNumbersCount; i++) { dataOut.writeInt(lineNumbers[i]); } } finally { IOUtils.closeQuietly(dataOut); } }
From source file:org.globus.myproxy.MyProxy.java
private InputStream handleReply(InputStream in, OutputStream out, GSSCredential authzcreds, boolean wantTrustroots) throws IOException, MyProxyException { String tmp = null;/* w ww . ja v a2 s .c om*/ /* there was something weird here with the received protocol version sometimes. it contains an extra <32 byte. fixed it by using endsWith. now i read extra byte at the end of each message. */ // protocol version tmp = readLine(in); if (tmp == null) { throw new EOFException(); } if (!tmp.endsWith(MyProxyConstants.VERSION)) { throw new MyProxyException("Protocol version mismatch: " + tmp); } // response tmp = readLine(in); if (tmp == null) { throw new EOFException(); } if (!tmp.startsWith(RESPONSE)) { throw new MyProxyException("Invalid reply: no response message"); } boolean error = tmp.charAt(RESPONSE.length()) == '1'; boolean authzchallenge = tmp.charAt(RESPONSE.length()) == '2'; if (error) { StringBuffer errorStr = new StringBuffer(); while ((tmp = readLine(in)) != null) { if (tmp.startsWith(ERROR)) { if (errorStr.length() > 0) errorStr.append(' '); errorStr.append(tmp.substring(ERROR.length())); } } if (errorStr.length() == 0) { errorStr.append("unspecified server error"); } throw new MyProxyException(errorStr.toString()); } if (authzchallenge) { if (authzcreds == null) { throw new MyProxyException( "Unable to respond to server's authentication challenge. No credentials for renewal."); } if (out == null) { throw new MyProxyException("Internal error. Authz challenge but no OutputStream."); } String[] authzdata = null; while ((tmp = readLine(in)) != null) { if (tmp.startsWith(AUTHZ_DATA)) { int pos = tmp.indexOf(':', AUTHZ_DATA.length() + 1); if (pos != -1) { authzdata = new String[2]; authzdata[0] = tmp.substring(AUTHZ_DATA.length(), pos).trim(); authzdata[1] = tmp.substring(pos + 1).trim(); } if (authzdata == null) { throw new MyProxyException("Unable to parse authorization challenge from server."); } if (authzdata[0].equals("X509_certificate")) { GlobusGSSCredentialImpl pkiCred = (GlobusGSSCredentialImpl) authzcreds; try { Signature sig = Signature.getInstance("SHA1withRSA"); sig.initSign(pkiCred.getPrivateKey()); sig.update(authzdata[1].getBytes()); byte[] sigbytes = sig.sign(); X509Certificate[] certs = pkiCred.getCertificateChain(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(2048); buffer.write(2); // AUTHORIZETYPE_CERT buffer.write(0); buffer.write(0); buffer.write(0); // pad DataOutputStream dos = new DataOutputStream(buffer); dos.writeInt(sigbytes.length); dos.flush(); buffer.write(sigbytes); buffer.write((byte) certs.length); for (int i = 0; i < certs.length; i++) { buffer.write(certs[i].getEncoded()); } out.write(buffer.toByteArray()); out.flush(); } catch (Exception e) { throw new MyProxyException("Authz response failed.", e); } } else { authzdata = null; continue; } } } return handleReply(in, out, authzcreds, wantTrustroots); } if (wantTrustroots == true) { while ((tmp = readLine(in)) != null) { if (tmp.startsWith(TRUSTROOTS)) { String filenameList = tmp.substring(TRUSTROOTS.length()); this.trustrootFilenames = filenameList.split(","); this.trustrootData = new String[this.trustrootFilenames.length]; for (int i = 0; i < this.trustrootFilenames.length; i++) { String lineStart = "FILEDATA_" + this.trustrootFilenames[i] + "="; tmp = readLine(in); if (tmp == null) { throw new EOFException(); } if (!tmp.startsWith(lineStart)) { throw new MyProxyException("bad MyProxy protocol RESPONSE: expecting " + lineStart + " but received " + tmp); } this.trustrootData[i] = new String( Base64.decode(tmp.substring(lineStart.length()).getBytes())); } } } } /* always consume the entire message */ int avail = in.available(); byte[] b = new byte[avail]; if (avail > 0) in.read(b); ByteArrayInputStream inn = new ByteArrayInputStream(b); return inn; }
From source file:com.linkedin.pinot.core.startree.OffHeapStarTreeBuilder.java
/** * Assumes the file is already sorted, returns the unique combinations after removing a specified * dimension./*from www. ja va2s . c o m*/ * Aggregates the metrics for each unique combination, currently only sum is supported by default * @param startDocId * @param endDocId * @param file * @param splitDimensionId * @return * @throws Exception */ private Iterator<Pair<DimensionBuffer, MetricBuffer>> uniqueCombinations(int startDocId, int endDocId, File file, int splitDimensionId) throws Exception { StarTreeDataTable dataSorter = new StarTreeDataTable(file, dimensionSizeBytes, metricSizeBytes, getSortOrder()); Iterator<Pair<byte[], byte[]>> iterator1 = dataSorter.iterator(startDocId, endDocId); File tempFile = new File(outDir, file.getName() + "_" + startDocId + "_" + endDocId + ".unique.tmp"); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile))); while (iterator1.hasNext()) { Pair<byte[], byte[]> next = iterator1.next(); byte[] dimensionBuffer = next.getLeft(); byte[] metricBuffer = next.getRight(); DimensionBuffer dimensions = DimensionBuffer.fromBytes(dimensionBuffer); for (int i = 0; i < numDimensions; i++) { String dimensionName = dimensionNameToIndexMap.inverse().get(i); if (i == splitDimensionId || (skipMaterializationForDimensions != null && skipMaterializationForDimensions.contains(dimensionName))) { dos.writeInt(StarTreeIndexNodeInterf.ALL); } else { dos.writeInt(dimensions.getDimension(i)); } } dos.write(metricBuffer); } dos.close(); dataSorter = new StarTreeDataTable(tempFile, dimensionSizeBytes, metricSizeBytes, getSortOrder()); dataSorter.sort(0, endDocId - startDocId); if (debugMode) { printFile(tempFile, 0, endDocId - startDocId); } final Iterator<Pair<byte[], byte[]>> iterator = dataSorter.iterator(0, endDocId - startDocId); return new Iterator<Pair<DimensionBuffer, MetricBuffer>>() { Pair<DimensionBuffer, MetricBuffer> prev = null; boolean done = false; @Override public void remove() { throw new UnsupportedOperationException(); } @Override public boolean hasNext() { return !done; } @Override public Pair<DimensionBuffer, MetricBuffer> next() { while (iterator.hasNext()) { Pair<byte[], byte[]> next = iterator.next(); byte[] dimBuffer = next.getLeft(); byte[] metricBuffer = next.getRight(); if (prev == null) { prev = Pair.of(DimensionBuffer.fromBytes(dimBuffer), MetricBuffer.fromBytes(metricBuffer, schema.getMetricFieldSpecs())); } else { Pair<DimensionBuffer, MetricBuffer> current = Pair.of(DimensionBuffer.fromBytes(dimBuffer), MetricBuffer.fromBytes(metricBuffer, schema.getMetricFieldSpecs())); if (!current.getLeft().equals(prev.getLeft())) { Pair<DimensionBuffer, MetricBuffer> ret = prev; prev = current; LOG.debug("Returning unique {}", prev.getLeft()); return ret; } else { prev.getRight().aggregate(current.getRight()); } } } done = true; LOG.debug("Returning unique {}", prev.getLeft()); return prev; } }; }
From source file:com.jivesoftware.os.amza.service.AmzaService.java
private boolean streamOnline(RingMember ringMember, VersionedPartitionName versionedPartitionName, long highestTransactionId, long leadershipToken, long limit, DataOutputStream dos, MutableLong bytes, HighwaterStorage highwaterStorage, PartitionStripe.RowStreamer streamer) throws Exception { ackWaters.set(ringMember, versionedPartitionName, highestTransactionId, leadershipToken); dos.writeLong(leadershipToken);/*www . j a va 2s .c o m*/ dos.writeLong(versionedPartitionName.getPartitionVersion()); dos.writeByte(1); // fully online bytes.increment(); RingTopology ring = ringStoreReader.getRing(versionedPartitionName.getPartitionName().getRingName(), -1); for (int i = 0; i < ring.entries.size(); i++) { if (ring.rootMemberIndex != i) { RingMemberAndHost entry = ring.entries.get(i); long highwatermark = highwaterStorage.get(entry.ringMember, versionedPartitionName); byte[] ringMemberBytes = entry.ringMember.toBytes(); dos.writeByte(1); dos.writeInt(ringMemberBytes.length); dos.write(ringMemberBytes); dos.writeLong(highwatermark); bytes.add(1 + 4 + ringMemberBytes.length + 8); } } dos.writeByte(0); // last entry marker bytes.increment(); long[] limited = new long[1]; long[] lastRowTxId = { -1 }; boolean streamedToEnd = streamer.stream((rowFP, rowTxId, rowType, row) -> { if (limited[0] >= limit && lastRowTxId[0] < rowTxId) { return false; } lastRowTxId[0] = rowTxId; dos.writeByte(1); dos.writeLong(rowTxId); dos.writeByte(rowType.toByte()); dos.writeInt(row.length); dos.write(row); bytes.add(1 + 8 + 1 + 4 + row.length); limited[0]++; return true; }); dos.writeByte(0); // last entry marker bytes.increment(); dos.writeByte(streamedToEnd ? 1 : 0); // streamedToEnd marker bytes.increment(); return false; }
From source file:org.apache.jmeter.protocol.mqtt.client.MqttPublisher.java
public byte[] createPayload(String message, String useTimeStamp, String useNumSeq, String type_value, String format, String charset) throws IOException, NumberFormatException { ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream d = new DataOutputStream(b); // flags byte flags = 0x00; if ("TRUE".equals(useTimeStamp)) flags |= 0x80;//from ww w. j a v a 2 s .c om if ("TRUE".equals(useNumSeq)) flags |= 0x40; if (MQTTPublisherGui.INT.equals(type_value)) flags |= 0x20; if (MQTTPublisherGui.LONG.equals(type_value)) flags |= 0x10; if (MQTTPublisherGui.FLOAT.equals(type_value)) flags |= 0x08; if (MQTTPublisherGui.DOUBLE.equals(type_value)) flags |= 0x04; if (MQTTPublisherGui.STRING.equals(type_value)) flags |= 0x02; if (!"TEXT".equals(type_value)) { d.writeByte(flags); } // TimeStamp if ("TRUE".equals(useTimeStamp)) { Date date = new java.util.Date(); d.writeLong(date.getTime()); } // Number Sequence if ("TRUE".equals(useNumSeq)) { d.writeInt(numSeq++); } // Value if (MQTTPublisherGui.INT.equals(type_value)) { d.writeInt(Integer.parseInt(message)); } else if (MQTTPublisherGui.LONG.equals(type_value)) { d.writeLong(Long.parseLong(message)); } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) { d.writeDouble(Double.parseDouble(message)); } else if (MQTTPublisherGui.FLOAT.equals(type_value)) { d.writeDouble(Float.parseFloat(message)); } else if (MQTTPublisherGui.STRING.equals(type_value)) { d.write(message.getBytes()); } else if ("TEXT".equals(type_value)) { d.write(message.getBytes()); } // Format: Encoding if (MQTTPublisherGui.BINARY.equals(format)) { BinaryCodec encoder = new BinaryCodec(); return encoder.encode(b.toByteArray()); } else if (MQTTPublisherGui.BASE64.equals(format)) { return Base64.encodeBase64(b.toByteArray()); } else if (MQTTPublisherGui.BINHEX.equals(format)) { Hex encoder = new Hex(); return encoder.encode(b.toByteArray()); } else if (MQTTPublisherGui.PLAIN_TEXT.equals(format)) { String s = new String(b.toByteArray(), charset); return s.getBytes(); } else return b.toByteArray(); }