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

The following examples show how to use java.io.DataOutput#writeBytes() . 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: HttpResponseImpl.java    From tomee with Apache License 2.0 6 votes vote down vote up
/**
 * writes the body out to the browser
 *
 * @param out    the output stream that writes to the browser
 * @param indent format xml
 * @throws java.io.IOException if an exception is thrown
 */
private void writeBody(final DataOutput out, final boolean indent) throws IOException {
    out.writeBytes(CRLF);
    if (content == null) {
        if (indent && OpenEJBHttpServer.isTextXml(headers)) {
            final String xml = new String(sosi.getOutputStream().toByteArray());
            out.write(OpenEJBHttpServer.reformat(xml).getBytes());
        } else {
            out.write(sosi.getOutputStream().toByteArray());
        }
    } else {
        final InputStream in = content.getInputStream();
        final byte[] buf = new byte[1024];

        int i;
        while ((i = in.read(buf)) != -1) {
            out.write(buf, 0, i);
        }
    }
}
 
Example 2
Source File: Bytes.java    From tac with MIT License 5 votes vote down vote up
/**
 * Writes a string as a fixed-size field, padded with zeros.
 */
public static void writeStringFixedSize(final DataOutput out, String s,
                                        int size) throws IOException {
    byte[] b = toBytes(s);
    if (b.length > size) {
        throw new IOException("Trying to write " + b.length + " bytes (" +
            toStringBinary(b) + ") into a field of length " + size);
    }

    out.writeBytes(s);
    for (int i = 0; i < size - s.length(); ++i) { out.writeByte(0); }
}
 
Example 3
Source File: HttpResponseImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a response line similar to this:
 *
 * HTTP/1.1 200 OK
 *
 * to the browser
 *
 * @param out the output stream to write the response line to
 * @throws java.io.IOException if an exception is thrown
 */
private void writeResponseLine(final DataOutput out) throws IOException {
    out.writeBytes(HTTP_VERSION);
    out.writeBytes(SP);
    out.writeBytes(code + "");
    out.writeBytes(SP);
    if (responseString != null) {
        out.writeBytes(responseString);
    }
    out.writeBytes(CRLF);
}
 
Example 4
Source File: TargetedTuple.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput out) throws IOException {
    t.write(out);
    out.writeInt(targetOps.size());
    for (OperatorKey target : targetOps) {
        // Ideally I should be able to call target.write(out).
        // Since it doesn't support it yet handling it here
        out.writeInt(target.scope.length());
        out.writeBytes(target.scope);
        out.writeLong(target.id);
    }
}
 
Example 5
Source File: Bytes.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a string as a fixed-size field, padded with zeros.
 */
public static void writeStringFixedSize(final DataOutput out, String s,
    int size) throws IOException {
  byte[] b = toBytes(s);
  if (b.length > size) {
    throw new IOException("Trying to write " + b.length + " bytes (" +
        toStringBinary(b) + ") into a field of length " + size);
  }

  out.writeBytes(s);
  for (int i = 0; i < size - s.length(); ++i)
    out.writeByte(0);
}
 
Example 6
Source File: Bytes.java    From tajo with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a string as a fixed-size field, padded with zeros.
 */
public static void writeStringFixedSize(final DataOutput out, String s,
                                        int size) throws IOException {
  byte[] b = toBytes(s);
  if (b.length > size) {
    throw new IOException("Trying to write " + b.length + " bytes (" +
        toStringBinary(b) + ") into a field of length " + size);
  }

  out.writeBytes(s);
  for (int i = 0; i < size - s.length(); ++i)
    out.writeByte(0);
}
 
Example 7
Source File: Bytes.java    From kylin with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a string as a fixed-size field, padded with zeros.
 */
public static void writeStringFixedSize(final DataOutput out, String s, int size) throws IOException {
    byte[] b = toBytes(s);
    if (b.length > size) {
        throw new IOException("Trying to write " + b.length + " bytes (" + toStringBinary(b)
                + ") into a field of length " + size);
    }

    out.writeBytes(s);
    for (int i = 0; i < size - s.length(); ++i)
        out.writeByte(0);
}
 
Example 8
Source File: HealthStatsMetricsSerializer.java    From Battery-Metrics with MIT License 5 votes vote down vote up
private static void writeString(@Nullable String str, DataOutput output) throws IOException {
  if (str == null) {
    output.writeInt(0);
  } else {
    output.writeInt(str.length());
    output.writeBytes(str);
  }
}
 
Example 9
Source File: TenantAndIdEmittableKey.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput data) throws IOException {
    data.writeBytes(getTenantIdField().toString() + "\n");
    data.writeBytes(getTenantId().toString() + "\n");
    data.writeBytes(getIdField().toString() + "\n");
    data.writeBytes(getId().toString() + "\n");
}
 
Example 10
Source File: DeltaObject.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected void _toDelta(DataOutput out) throws IOException {
   Log.getLogWriter().info("In DeltaObject.toDelta, for " + this + "\n" + "with DataOutput " + out);
   DeltaObserver.addToDeltaKey(extra);

   // for validation later, encode with a toDelta id number
   long toDeltaIdNumber = DeltaPropagationBB.getBB().getSharedCounters().incrementAndRead(DeltaPropagationBB.toDeltaIdNumber);
   out.writeLong(toDeltaIdNumber);
   Log.getLogWriter().info("Wrote toDeltaIdNumber " + toDeltaIdNumber);

   out.writeBoolean(aPrimitiveLongChanged);
   if (aPrimitiveLongChanged) {
      out.writeLong(aPrimitiveLong);
      Log.getLogWriter().info("Wrote changed field aPrimitiveLong " + aPrimitiveLong);
   }
   out.writeBoolean(aPrimitiveIntChanged);
   if (aPrimitiveIntChanged) {
      out.writeInt(aPrimitiveInt);
      Log.getLogWriter().info("Wrote changed field aPrimitiveInt " + aPrimitiveInt);
   }
   out.writeBoolean(aPrimitiveShortChanged);
   if (aPrimitiveShortChanged) {
      out.writeShort(aPrimitiveShort);
      Log.getLogWriter().info("Wrote changed field aPrimitiveShort " + aPrimitiveShort);
   }
   out.writeBoolean(aPrimitiveFloatChanged);
   if (aPrimitiveFloatChanged) {
      out.writeFloat(aPrimitiveFloat);
      Log.getLogWriter().info("Wrote changed field aPrimitiveFloat " + aPrimitiveFloat);
   }
   out.writeBoolean(aPrimitiveDoubleChanged);
   if (aPrimitiveDoubleChanged) {
      out.writeDouble(aPrimitiveDouble);
      Log.getLogWriter().info("Wrote changed field aPrimitiveDouble " + aPrimitiveDouble);
   }
   out.writeBoolean(aPrimitiveByteChanged);
   if (aPrimitiveByteChanged) {
      out.writeByte(aPrimitiveByte);
      Log.getLogWriter().info("Wrote changed field aPrimitiveByte " + aPrimitiveByte);
   }
   out.writeBoolean(aPrimitiveCharChanged);
   if (aPrimitiveCharChanged) {
      out.writeChar(aPrimitiveChar);
      Log.getLogWriter().info("Wrote changed field aPrimitiveChar " + aPrimitiveChar);
   }
   out.writeBoolean(aPrimitiveBooleanChanged);
   if (aPrimitiveBooleanChanged) {
      out.writeBoolean(aPrimitiveBoolean);
      Log.getLogWriter().info("Wrote changed field aPrimitiveBoolean " + aPrimitiveBoolean);
   }
   out.writeBoolean(aByteArrayChanged);
   if (aByteArrayChanged) {
      out.writeInt(aByteArray.length);
      out.write(aByteArray);
      Log.getLogWriter().info("Wrote changed field aByteArray of length " + aByteArray.length);
   }
   out.writeBoolean(aStringChanged);
   if (aStringChanged) {
      out.writeInt(aString.length());
      out.writeBytes(aString);
      Log.getLogWriter().info("Wrote changed field aString " + aString);
   }
   out.writeInt(END_SIGNAL.length());
   out.writeBytes(END_SIGNAL);
   Log.getLogWriter().info("Wrote end signal: " + END_SIGNAL);
   Log.getLogWriter().info("Done writing in DeltaObject.toDelta for " + this);
}
 
Example 11
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 12
Source File: DeltaObject.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected void _toDelta(DataOutput out) throws IOException {
   Log.getLogWriter().info("In DeltaObject.toDelta, for " + this + "\n" + "with DataOutput " + out);
   DeltaObserver.addToDeltaKey(extra);

   // for validation later, encode with a toDelta id number
   long toDeltaIdNumber = DeltaPropagationBB.getBB().getSharedCounters().incrementAndRead(DeltaPropagationBB.toDeltaIdNumber);
   out.writeLong(toDeltaIdNumber);
   Log.getLogWriter().info("Wrote toDeltaIdNumber " + toDeltaIdNumber);

   out.writeBoolean(aPrimitiveLongChanged);
   if (aPrimitiveLongChanged) {
      out.writeLong(aPrimitiveLong);
      Log.getLogWriter().info("Wrote changed field aPrimitiveLong " + aPrimitiveLong);
   }
   out.writeBoolean(aPrimitiveIntChanged);
   if (aPrimitiveIntChanged) {
      out.writeInt(aPrimitiveInt);
      Log.getLogWriter().info("Wrote changed field aPrimitiveInt " + aPrimitiveInt);
   }
   out.writeBoolean(aPrimitiveShortChanged);
   if (aPrimitiveShortChanged) {
      out.writeShort(aPrimitiveShort);
      Log.getLogWriter().info("Wrote changed field aPrimitiveShort " + aPrimitiveShort);
   }
   out.writeBoolean(aPrimitiveFloatChanged);
   if (aPrimitiveFloatChanged) {
      out.writeFloat(aPrimitiveFloat);
      Log.getLogWriter().info("Wrote changed field aPrimitiveFloat " + aPrimitiveFloat);
   }
   out.writeBoolean(aPrimitiveDoubleChanged);
   if (aPrimitiveDoubleChanged) {
      out.writeDouble(aPrimitiveDouble);
      Log.getLogWriter().info("Wrote changed field aPrimitiveDouble " + aPrimitiveDouble);
   }
   out.writeBoolean(aPrimitiveByteChanged);
   if (aPrimitiveByteChanged) {
      out.writeByte(aPrimitiveByte);
      Log.getLogWriter().info("Wrote changed field aPrimitiveByte " + aPrimitiveByte);
   }
   out.writeBoolean(aPrimitiveCharChanged);
   if (aPrimitiveCharChanged) {
      out.writeChar(aPrimitiveChar);
      Log.getLogWriter().info("Wrote changed field aPrimitiveChar " + aPrimitiveChar);
   }
   out.writeBoolean(aPrimitiveBooleanChanged);
   if (aPrimitiveBooleanChanged) {
      out.writeBoolean(aPrimitiveBoolean);
      Log.getLogWriter().info("Wrote changed field aPrimitiveBoolean " + aPrimitiveBoolean);
   }
   out.writeBoolean(aByteArrayChanged);
   if (aByteArrayChanged) {
      out.writeInt(aByteArray.length);
      out.write(aByteArray);
      Log.getLogWriter().info("Wrote changed field aByteArray of length " + aByteArray.length);
   }
   out.writeBoolean(aStringChanged);
   if (aStringChanged) {
      out.writeInt(aString.length());
      out.writeBytes(aString);
      Log.getLogWriter().info("Wrote changed field aString " + aString);
   }
   out.writeInt(END_SIGNAL.length());
   out.writeBytes(END_SIGNAL);
   Log.getLogWriter().info("Wrote end signal: " + END_SIGNAL);
   Log.getLogWriter().info("Done writing in DeltaObject.toDelta for " + this);
}
 
Example 13
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 14
Source File: L10.java    From spork with Apache License 2.0 4 votes vote down vote up
public void write(DataOutput out) throws IOException {
    out.writeInt(timespent);
    out.writeDouble(estimated_revenue);
    out.writeInt(query_term.length());
    out.writeBytes(query_term);
}
 
Example 15
Source File: Config.java    From seventh with GNU General Public License v2.0 4 votes vote down vote up
private void writeTabs(DataOutput output, int numTabs) throws IOException {
    for(int i = 0; i < numTabs; i++) {
        output.writeBytes("\t");
    }
}
 
Example 16
Source File: Config.java    From seventh with GNU General Public License v2.0 4 votes vote down vote up
private void writeLn(DataOutput output, String txt, int numTabs) throws IOException {
    writeTabs(output, numTabs);
    output.writeBytes(txt);
}
 
Example 17
Source File: HttpResponseImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
private void writeHeader(final DataOutput out, final String name, final String value) throws IOException {
    out.writeBytes(name);
    out.writeBytes(CSP);
    out.writeBytes(value);
    out.writeBytes(CRLF);
}
 
Example 18
Source File: IdFieldEmittableKey.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
@Override
public void write(DataOutput data) throws IOException {
    data.writeBytes(getIdField().toString() + "\n");
    data.writeBytes(getId().toString() + "\n");
}
 
Example 19
Source File: ExchangeTest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
private static void runClientMsgComm(final DataInput in, final DataOutput out) throws IOException {
  System.out.println("client says 'hello'");

  out.writeBytes("hello\n");
  out.writeBytes("I say po-ta-toe, you say...\n");

  String value = in.readLine();

  System.out.printf("client: server said '%1$s'%n", value);
}
 
Example 20
Source File: ExchangeTest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
private static void runClientMsgComm(final DataInput in, final DataOutput out) throws IOException {
  System.out.println("client says 'hello'");

  out.writeBytes("hello\n");
  out.writeBytes("I say po-ta-toe, you say...\n");

  String value = in.readLine();

  System.out.printf("client: server said '%1$s'%n", value);
}