Java Code Examples for java.io.DataOutputStream#size()

The following examples show how to use java.io.DataOutputStream#size() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: TimableEventQueue.java    License: Apache License 2.0 6 votes vote down vote up
private static void report(final Sampler ss, final long time) {
    if (ss == null) {
        return;
    }
    class R implements Runnable {
        @Override
        public void run() {
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(out);
                ss.stopAndWriteTo(dos);
                dos.close();
                if (dos.size() > 0) {
                    Object[] params = new Object[]{out.toByteArray(), time};
                    Logger.getLogger("org.netbeans.ui.performance").log(Level.CONFIG, "Slowness detected", params);
                } else {
                    LOG.log(Level.WARNING, "no snapshot taken"); // NOI18N
                }
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    RP.post(new R());
}
 
Example 2
Source Project: netbeans   File: GoToTypeAction.java    License: Apache License 2.0 6 votes vote down vote up
private synchronized void stop() throws Exception {
    long delta = System.currentTimeMillis() - time;

    Sampler ss = profiler;
    profiler = null;
    if (!profiling) {
        return;
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        ss.stopAndWriteTo(dos);
        dos.close();
        if (dos.size() > 0) {
            Object[] params = new Object[]{out.toByteArray(), delta, "GoToType" };      //NOI18N
            Logger.getLogger("org.netbeans.ui.performance").log(Level.CONFIG, "Slowness detected", params); //NOI18N
        } else {
            LOGGER.log(Level.WARNING, "no snapshot taken"); // NOI18N
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 3
private void writeFrame(final DataOutputStream dos) throws IOException {

      dos.writeByte(serializerType);
      dos.writeInt(maximumMessageLength);
      SerializerUtils.writeInetAddress(senderInetAddress, dos, true);
      dos.writeByte(compressionType);
      dos.writeLong(sequenceNumber);
      dos.writeInt(partCount);
      dos.writeInt(partIndex);
      SerializerUtils.writeUuid(clusterUUID, dos);
      dos.writeInt(payloadLength);
      dos.write(payload);
      if (dos.size() > maximumMessageLength) {
         throw new InvalidObjectException(
                 "Message length exceeded maximum allowed " + maximumMessageLength + ": " + dos.size());
      }
   }
 
Example 4
Source Project: tinydnssd   File: MDNSDiscover.java    License: MIT License 6 votes vote down vote up
static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(0);
    dos.writeShort(qtypes.length);  // questions
    dos.writeShort(0);  // answers
    dos.writeShort(0);  // nscount
    dos.writeShort(0);  // arcount
    int fqdnPtr = -1;
    for (int qtype : qtypes) {
        if (fqdnPtr == -1) {
            fqdnPtr = dos.size();
            writeFQDN(serviceName, dos);
        } else {
            // packet compression, string is just a pointer to previous occurrence
            dos.write(0xc0 | (fqdnPtr >> 8));
            dos.write(fqdnPtr & 0xFF);
        }
        dos.writeShort(qtype);
        dos.writeShort(qclass);
    }
    dos.close();
    return bos.toByteArray();
}
 
Example 5
/**
 * Used internally for performance information gathering; not intended for
 * public use. Serializes the message in AMFX format and returns the size
 * of the serialized message.
 *
 * @param message Message to get the size for.
 *
 * @return The size of the message after message is serialized.
 */
@Override protected long getMessageSizeForPerformanceInfo(Message message)
{
    AmfxOutput amfxOut = new AmfxOutput(serializationContext);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DataOutputStream dataOutStream = new DataOutputStream(outStream);
    amfxOut.setOutputStream(dataOutStream);
    try
    {
        amfxOut.writeObject(message);
    }
    catch (IOException e)
    {
        if (Log.isDebug())
            log.debug("MPI exception while retrieving the size of the serialized message: " + e.toString());
    }
    return dataOutStream.size();
}
 
Example 6
/**
 * Used internally for performance information gathering; not intended for
 * public use. Serializes the message in AMF format and returns the size of
 * the serialized message.
 *
 * @param message Message to get the size for.
 *
 * @return The size of the message after message is serialized.
 */
@Override protected long getMessageSizeForPerformanceInfo(Message message)
{
    Amf3Output amfOut = new Amf3Output(serializationContext);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DataOutputStream dataOutStream = new DataOutputStream(outStream);
    amfOut.setOutputStream(dataOutStream);
    try
    {
        amfOut.writeObject(message);
    }
    catch (IOException e)
    {
        if (Log.isDebug())
            log.debug("MPI exception while retrieving the size of the serialized message: " + e.toString());
    }
    return dataOutStream.size();
}
 
Example 7
public static <T extends RootPersistentEntity> ContentWriter create(final T obj, final Serializer<T> serializer) {
    return new ContentWriter() {
        @Override
        void write(DataOutputStream out) throws IOException {
            int pos = out.size();
            serializer.serialize(obj, out);
            bytesWritten += (out.size() - pos);
        }
    };
}
 
Example 8
Source Project: netbeans   File: CompletionImplProfile.java    License: Apache License 2.0 5 votes vote down vote up
private void stopImpl(long now) throws Exception {
    long delta = now - time;
    LOG.log(Level.FINE, "Profiling stopped at {0}", now);
    int report = Integer.getInteger("org.netbeans.modules.editor.completion.slowness.report", 2000); // NOI18N
    if (delta < report) {
        LOG.log(Level.FINE, "Cancel profiling of {0}. Profiling {1}. Time {2} ms.", new Object[] { profiler, profiling, delta });
        if (profiler != null) {
            profiler.cancel();
        }
        return;
    }
    try {
        LOG.log(Level.FINE, "Obtaining snapshot for {0} ms.", delta);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        if (profiler != null) {
            profiler.stopAndWriteTo(dos);
        }
        dos.close();
        if (dos.size() > 0) {
            Object[] params = new Object[]{out.toByteArray(), delta, "CodeCompletion"};
            Logger.getLogger("org.netbeans.ui.performance").log(Level.CONFIG, "Slowness detected", params);
            LOG.log(Level.FINE, "Snapshot sent to UI logger. Size {0} bytes.", dos.size());
        } else {
            LOG.log(Level.WARNING, "No snapshot taken"); // NOI18N
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 9
Source Project: joshua   File: GrammarPacker.java    License: Apache License 2.0 5 votes vote down vote up
private void writeHeader(DataOutputStream out) throws IOException {
  if (out.size() == 0) {
    out.writeInt(onDiskOrder.size());
    out.writeInt(totalSize);
    int disk_position = headerSize();
    for (int block_index : onDiskOrder) {
      out.writeInt(disk_position);
      disk_position += blockSize(block_index);
    }
  } else {
    throw new RuntimeException("Got a used stream for header writing.");
  }
}
 
Example 10
Source Project: kylin   File: ContentWriter.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends RootPersistentEntity> ContentWriter create(final T obj, final Serializer<T> serializer) {
    return new ContentWriter() {
        @Override
        void write(DataOutputStream out) throws IOException {
            int pos = out.size();
            serializer.serialize(obj, out);
            bytesWritten += (out.size() - pos);
        }
    };
}
 
Example 11
protected final long PADSAVEP(DataOutputStream f) throws IOException {
    long save_p = f.size();
    int padding = (4 - ((int) save_p & 3)) & 3;
    // System.out.printf("Current position %d Padding by %d bytes\n",save_p,padding);
    for (int i = 0; i < padding; i++) {
        f.write(0);
    }
    return padding;
}
 
Example 12
Source Project: hbase   File: HFileBlockIndex.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Used when writing the root block index of a multi-level block index.
 * Serializes additional information allowing to efficiently identify the
 * mid-key.
 *
 * @return a few serialized fields for finding the mid-key
 * @throws IOException if could not create metadata for computing mid-key
 */
public byte[] getMidKeyMetadata() throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream(
      MID_KEY_METADATA_SIZE);
  DataOutputStream baosDos = new DataOutputStream(baos);
  long totalNumSubEntries = numSubEntriesAt.get(blockKeys.size() - 1);
  if (totalNumSubEntries == 0) {
    throw new IOException("No leaf-level entries, mid-key unavailable");
  }
  long midKeySubEntry = (totalNumSubEntries - 1) / 2;
  int midKeyEntry = getEntryBySubEntry(midKeySubEntry);

  baosDos.writeLong(blockOffsets.get(midKeyEntry));
  baosDos.writeInt(onDiskDataSizes.get(midKeyEntry));

  long numSubEntriesBefore = midKeyEntry > 0
      ? numSubEntriesAt.get(midKeyEntry - 1) : 0;
  long subEntryWithinEntry = midKeySubEntry - numSubEntriesBefore;
  if (subEntryWithinEntry < 0 || subEntryWithinEntry > Integer.MAX_VALUE)
  {
    throw new IOException("Could not identify mid-key index within the "
        + "leaf-level block containing mid-key: out of range ("
        + subEntryWithinEntry + ", numSubEntriesBefore="
        + numSubEntriesBefore + ", midKeySubEntry=" + midKeySubEntry
        + ")");
  }

  baosDos.writeInt((int) subEntryWithinEntry);

  if (baosDos.size() != MID_KEY_METADATA_SIZE) {
    throw new IOException("Could not write mid-key metadata: size=" +
        baosDos.size() + ", correct size: " + MID_KEY_METADATA_SIZE);
  }

  // Close just to be good citizens, although this has no effect.
  baos.close();

  return baos.toByteArray();
}
 
Example 13
Source Project: hbase   File: EncodedDataBlock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Find the size of compressed data assuming that buffer will be compressed
 * using given algorithm.
 * @param algo compression algorithm
 * @param compressor compressor already requested from codec
 * @param inputBuffer Array to be compressed.
 * @param offset Offset to beginning of the data.
 * @param length Length to be compressed.
 * @return Size of compressed data in bytes.
 * @throws IOException
 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH_EXCEPTION",
     justification="No sure what findbugs wants but looks to me like no NPE")
public static int getCompressedSize(Algorithm algo, Compressor compressor,
    byte[] inputBuffer, int offset, int length) throws IOException {

  // Create streams
  // Storing them so we can close them
  final IOUtils.NullOutputStream nullOutputStream = new IOUtils.NullOutputStream();
  final DataOutputStream compressedStream = new DataOutputStream(nullOutputStream);
  OutputStream compressingStream = null;


  try {
    if (compressor != null) {
      compressor.reset();
    }

    compressingStream = algo.createCompressionStream(compressedStream, compressor, 0);

    compressingStream.write(inputBuffer, offset, length);
    compressingStream.flush();

    return compressedStream.size();
  } finally {
    nullOutputStream.close();
    compressedStream.close();
    if (compressingStream != null) {
      compressingStream.close();
    }
  }
}
 
Example 14
Source Project: hbase   File: BufferedDataBlockEncoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encode(Cell cell, HFileBlockEncodingContext encodingCtx, DataOutputStream out)
    throws IOException {
  EncodingState state = encodingCtx.getEncodingState();
  int posBeforeEncode = out.size();
  int encodedKvSize = internalEncode(cell, (HFileBlockDefaultEncodingContext) encodingCtx, out);
  state.postCellEncode(encodedKvSize, out.size() - posBeforeEncode);
}
 
Example 15
Source Project: netbeans   File: SelfProfile.java    License: Apache License 2.0 4 votes vote down vote up
private void stopImpl() throws Exception {
    final long now = System.currentTimeMillis();
    long delta = now - time;
    LOG.log(Level.FINE, "Profiling stopped at {0}", now);
    int report = Integer.getInteger("org.netbeans.modules.parsing.api.taskcancel.slowness.report", 1000); // NOI18N
    if (delta < report) {
        LOG.finest("CANCEL");  //NOI18N
        if (profiler != null) {
            profiler.cancel();
            LOG.log(
                Level.FINE,
                "Cancel profiling of {0}. Profiling {1}. Time {2} ms.",     //NOI18N
                new Object[] {
                    profiler,
                    profiling,
                    delta
                });
        }
        return;
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        LOG.finest("LOGGED");  //NOI18N
        if (profiler != null) {
            profiler.stopAndSnapshot(dos);
            LOG.log(
                Level.FINE,
                "Obtaining snapshot for {0} ms.",   //NOI18N
                delta);
        }
        dos.close();
        if (dos.size() > 0) {
            Object[] params = new Object[]{out.toByteArray(), delta, "ParserResultTask-cancel"};    //NOI18N
            Logger.getLogger("org.netbeans.ui.performance").log(Level.CONFIG, "Slowness detected", params); //NOI18N
            LOG.log(Level.FINE, "Snapshot sent to UI logger. Size {0} bytes.", dos.size()); //NOI18N
        } else {
            LOG.log(Level.WARNING, "No snapshot taken"); // NOI18N
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 16
Source Project: PalDB   File: StorageWriter.java    License: Apache License 2.0 4 votes vote down vote up
private void writeMetadata(DataOutputStream dataOutputStream)
    throws IOException {
  //Write format version
  dataOutputStream.writeUTF(FormatVersion.getLatestVersion().name());

  //Write time
  dataOutputStream.writeLong(System.currentTimeMillis());

  //Prepare
  int keyLengthCount = getNumKeyCount();
  int maxKeyLength = keyCounts.length - 1;

  //Write size (number of keys)
  dataOutputStream.writeInt(keyCount);

  //Write the number of different key length
  dataOutputStream.writeInt(keyLengthCount);

  //Write the max value for keyLength
  dataOutputStream.writeInt(maxKeyLength);

  // For each keyLength
  long datasLength = 0l;
  for (int i = 0; i < keyCounts.length; i++) {
    if (keyCounts[i] > 0) {
      // Write the key length
      dataOutputStream.writeInt(i);

      // Write key count
      dataOutputStream.writeInt(keyCounts[i]);

      // Write slot count
      int slots = (int) Math.round(keyCounts[i] / loadFactor);
      dataOutputStream.writeInt(slots);

      // Write slot size
      int offsetLength = maxOffsetLengths[i];
      dataOutputStream.writeInt(i + offsetLength);

      // Write index offset
      dataOutputStream.writeInt((int) indexesLength);

      // Increment index length
      indexesLength += (i + offsetLength) * slots;

      // Write data length
      dataOutputStream.writeLong(datasLength);

      // Increment data length
      datasLength += dataLengths[i];
    }
  }

  //Write serializers
  try {
    Serializers.serialize(dataOutputStream, config.getSerializers());
  } catch (Exception e) {
    throw new RuntimeException();
  }

  //Write the position of the index and the data
  int indexOffset = dataOutputStream.size() + (Integer.SIZE / Byte.SIZE) + (Long.SIZE / Byte.SIZE);
  dataOutputStream.writeInt(indexOffset);
  dataOutputStream.writeLong(indexOffset + indexesLength);
}
 
Example 17
Source Project: uima-uimaj   File: BinaryCasSerDes4.java    License: Apache License 2.0 4 votes vote down vote up
/** 
     * Write the compressed string table(s)
     * @throws IOException
     */
    private void writeStringInfo() throws IOException {
      String [] commonStrings = os.getCommonStrings();
      writeVnumber(strChars_i, commonStrings.length);
      DataOutputStream out = dosZipSources[strChars_i];
      for (int i = 0; i < commonStrings.length; i++) {
        int startPos = doMeasurements ? out.size() : 0;
        DataIO.writeUTFv(commonStrings[i], out);
        // approximate histogram
        if (doMeasurements) {
          // len is utf-8 encoding
          float len = out.size() - startPos;
          // if len == chars, then all got coded as 1 byte
          // if len > chars, some were utf-8 coded as 2 bytes
          float excess = (len / commonStrings[i].length()) - 1;  // excess over length 1
          int encAs2 = (int)(excess * commonStrings[i].length());
          
          // simulate histo for all the chars, as 1 or 2 byte UTF8 encoding 
          sm.statDetails[strChars_i].countTotal += commonStrings[i].length(); // total chars accum
          sm.statDetails[strChars_i].c[0] = commonStrings[i].length() - encAs2;
          sm.statDetails[strChars_i].c[1] = encAs2;
          sm.statDetails[strChars_i].lengthTotal += len;  // total as UTF-8 encode
        }
      }
      
      only1CommonString = commonStrings.length == 1;

      if (doMeasurements) {
//        long commonStringsLength = 0;
//        sm.stringsNbrCommon = commonStrings.length;
//        int r = 0;
//        for (int i = 0; i < commonStrings.length; i++) {
//          r += DataIO.lengthUTFv(commonStrings[i]);
//          commonStringsLength += commonStrings[i].length();
//        }
//        sm.stringsCommonChars = r;
  //
//        sm.stringsSavedExact = os.getSavedCharsExact() * 2;
//        sm.stringsSavedSubstr = os.getSavedCharsSubstr() * 2;
//        sm.statDetails[strChars_i].original = os.getSavedCharsExact() * 2
//                                 + os.getSavedCharsSubstr() * 2
//                                 + commonStringsLength * 2;
//        final int stringHeapStart = isSerializingDelta ? mark.nextFSId : 1;
//        final int stringHeapEnd = stringHeapObj.getSize();
//        sm.statDetails[strLength_i].original = (stringHeapEnd - stringHeapStart) * 4;
//        sm.statDetails[strOffset_i].original = (stringHeapEnd - stringHeapStart) * 4;
      }

    }