Java Code Examples for java.io.DataOutput#writeChars()

The following examples show how to use java.io.DataOutput#writeChars() . 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 File: WakeLockMetricsSerializer.java    From Battery-Metrics with MIT License 6 votes vote down vote up
@Override
public void serializeContents(WakeLockMetrics metrics, DataOutput output) throws IOException {
  output.writeLong(metrics.heldTimeMs);
  output.writeLong(metrics.acquiredCount);
  output.writeBoolean(metrics.isAttributionEnabled);
  if (metrics.isAttributionEnabled) {
    int size = metrics.tagTimeMs.size();
    output.writeInt(size);
    for (int i = 0; i < size; i++) {
      String key = metrics.tagTimeMs.keyAt(i);
      long value = metrics.tagTimeMs.valueAt(i);
      output.writeInt(key.length());
      output.writeChars(key);
      output.writeLong(value);
    }
  }
}
 
Example 2
Source File: AppWakeupMetricsSerializer.java    From Battery-Metrics with MIT License 5 votes vote down vote up
@Override
public void serializeContents(AppWakeupMetrics metrics, DataOutput output) throws IOException {
  output.writeInt(metrics.appWakeups.size());
  for (int i = 0; i < metrics.appWakeups.size(); i++) {
    String wakeupName = metrics.appWakeups.keyAt(i);
    AppWakeupMetrics.WakeupDetails details = metrics.appWakeups.valueAt(i);
    output.writeInt(wakeupName.length());
    output.writeChars(wakeupName);
    output.writeInt(details.reason.ordinal());
    output.writeLong(details.count);
    output.writeLong(details.wakeupTimeMs);
  }
}
 
Example 3
Source File: VerifyHdfsDataUsingMR.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput out) throws IOException {
  out.writeInt(cid);
  out.writeChars(cname);
  out.writeChars(addr);
  out.writeInt(tid);
}
 
Example 4
Source File: VerifyHdfsDataUsingMR.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput out) throws IOException {
  out.writeInt(cid);
  out.writeChars(cname);
  out.writeChars(addr);
  out.writeInt(tid);
}
 
Example 5
Source File: WorkerContext.java    From Arabesque with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput dataOutput) throws IOException {
    super.write(dataOutput);

    dataOutput.writeInt(data.size());

    for (Map.Entry<String, String> dataEntry : data.entrySet()) {
        dataOutput.writeInt(dataEntry.getKey().length());
        dataOutput.writeChars(dataEntry.getKey());

        dataOutput.writeInt(dataEntry.getValue().length());
        dataOutput.writeChars(dataEntry.getValue());
    }
}
 
Example 6
Source File: DataSerializer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Writes an instance of <code>String</code> to a
 * <code>DataOutput</code>.
 * This method will handle a
 * <code>null</code> value and not throw a
 * <code>NullPointerException</code>.
 * <p>As of 5.7 strings longer than 0xFFFF can be serialized.
 *
 * @throws IOException
 *         A problem occurs while writing to <code>out</code>
 *
 * @see #readString
 */
public static void writeString(String value, DataOutput out)
  throws IOException {

  InternalDataSerializer.checkOut(out);

  if (DEBUG) {
    InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing String \"" + value + "\"");
  }

  if (value == null) {
    if (DEBUG) {
      InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing NULL_STRING");
    }
    out.writeByte(DSCODE.NULL_STRING);

  } else {
    // [bruce] writeUTF is expensive - it creates a char[] to fetch
    // the string's contents, iterates over the array to compute the
    // encoded length, creates a byte[] to hold the encoded bytes,
    // iterates over the char[] again to create the encode bytes,
    // then writes the bytes.  Since we usually deal with ISO-8859-1
    // strings, we can accelerate this by accessing chars directly
    // with charAt and fill a single-byte buffer.  If we run into
    // a multibyte char, we revert to using writeUTF()
    int len = value.length();
    int utfLen = len; // added for bug 40932
    for (int i=0; i<len; i++) {
      char c = value.charAt(i);
      if ((c <= 0x007F) && (c >= 0x0001)) {
        // nothing needed
      } else if (c > 0x07FF) {
        utfLen += 2;
      } else {
        utfLen += 1;
      }
      // Note we no longer have an early out when we detect the first
      // non-ascii char because we need to compute the utfLen for bug 40932.
      // This is not a performance problem because most strings are ascii
      // and they never did the early out.
    }
    boolean writeUTF = utfLen > len;
    if (writeUTF) {
      if (utfLen > 0xFFFF) {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf HUGE_STRING of len="+len);
        }
        out.writeByte(DSCODE.HUGE_STRING);
        out.writeInt(len);
        out.writeChars(value);
      } else {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf STRING of len="+len);
        }
        out.writeByte(DSCODE.STRING);
        out.writeUTF(value);
      }
    }
    else {
      if (len > 0xFFFF) {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing HUGE_STRING_BYTES of len="+len);
        }
        out.writeByte(DSCODE.HUGE_STRING_BYTES);
        out.writeInt(len);
        out.writeBytes(value);
      } else {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing STRING_BYTES of len="+len);
        }
        out.writeByte(DSCODE.STRING_BYTES);
        out.writeShort(len);
        out.writeBytes(value);
      }
    }
  }
}
 
Example 7
Source File: TestRowDBWritable.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void write(DataOutput out) throws IOException {
  out.writeInt(id);
  out.writeChars(name);
}
 
Example 8
Source File: DataSerializer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Writes an instance of <code>String</code> to a
 * <code>DataOutput</code>.
 * This method will handle a
 * <code>null</code> value and not throw a
 * <code>NullPointerException</code>.
 * <p>As of 5.7 strings longer than 0xFFFF can be serialized.
 *
 * @throws IOException
 *         A problem occurs while writing to <code>out</code>
 *
 * @see #readString
 */
public static void writeString(String value, DataOutput out)
  throws IOException {

  InternalDataSerializer.checkOut(out);

  if (DEBUG) {
    InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing String \"" + value + "\"");
  }

  if (value == null) {
    if (DEBUG) {
      InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing NULL_STRING");
    }
    out.writeByte(DSCODE.NULL_STRING);

  } else {
    // [bruce] writeUTF is expensive - it creates a char[] to fetch
    // the string's contents, iterates over the array to compute the
    // encoded length, creates a byte[] to hold the encoded bytes,
    // iterates over the char[] again to create the encode bytes,
    // then writes the bytes.  Since we usually deal with ISO-8859-1
    // strings, we can accelerate this by accessing chars directly
    // with charAt and fill a single-byte buffer.  If we run into
    // a multibyte char, we revert to using writeUTF()
    int len = value.length();
    int utfLen = len; // added for bug 40932
    for (int i=0; i<len; i++) {
      char c = value.charAt(i);
      if ((c <= 0x007F) && (c >= 0x0001)) {
        // nothing needed
      } else if (c > 0x07FF) {
        utfLen += 2;
      } else {
        utfLen += 1;
      }
      // Note we no longer have an early out when we detect the first
      // non-ascii char because we need to compute the utfLen for bug 40932.
      // This is not a performance problem because most strings are ascii
      // and they never did the early out.
    }
    boolean writeUTF = utfLen > len;
    if (writeUTF) {
      if (utfLen > 0xFFFF) {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf HUGE_STRING of len="+len);
        }
        out.writeByte(DSCODE.HUGE_STRING);
        out.writeInt(len);
        out.writeChars(value);
      } else {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf STRING of len="+len);
        }
        out.writeByte(DSCODE.STRING);
        out.writeUTF(value);
      }
    }
    else {
      if (len > 0xFFFF) {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing HUGE_STRING_BYTES of len="+len);
        }
        out.writeByte(DSCODE.HUGE_STRING_BYTES);
        out.writeInt(len);
        out.writeBytes(value);
      } else {
        if (DEBUG) {
          InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing STRING_BYTES of len="+len);
        }
        out.writeByte(DSCODE.STRING_BYTES);
        out.writeShort(len);
        out.writeBytes(value);
      }
    }
  }
}
 
Example 9
Source File: RyaTypeWritable.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final DataOutput dataOutput) throws IOException {
    dataOutput.writeChars(ryatype.getData());
    dataOutput.writeChars(ryatype.getDataType().toString());
    dataOutput.writeChars(ryatype.getLanguage());
}