public void write(int b) {
   try {
     dataOutput.write(b);
   } catch (IOException ex) {
     throw new RuntimeException(ex.getMessage());
   }
 }
  /**
   * DO NOT call symbolTable.addReference in writeObject as this (may) result in a
   * concurrentModificationException.
   *
   * <p>All references should be created in the constructor
   */
  public void writeObject(DataOutput out) throws IOException {
    boolean sgIO = node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO;
    out.writeBoolean(sgIO);
    out.writeInt(symbol.nodeID);

    int nodeClassID = control.getNodeClassID(node);

    out.writeShort(nodeClassID);

    if (nodeClassID == -1) out.writeUTF(nodeClassName);

    writeConstructorParams(out);

    if (sgIO) {
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      DataOutputStream tmpOut = new DataOutputStream(byteStream);
      ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO) node).writeSceneGraphObject(tmpOut);
      tmpOut.close();
      out.writeInt(byteStream.size());
      out.write(byteStream.toByteArray());
    }

    writeUserData(out);
    writeString(node.getName(), out);

    writeCapabilities(out);
  }
 public void write(byte[] b, int off, int len) {
   try {
     dataOutput.write(b, off, len);
   } catch (IOException ex) {
     throw new RuntimeException(ex.getMessage());
   }
 }
 public void write(DataOutput out) throws IOException {
   out.write(UNION_TYPE);
   out.writeUTF(name);
   out.writeInt(unionTypes.size());
   for (InferredType it : unionTypes) {
     it.write(out);
   }
 }
 public void write(DataOutput out) throws IOException {
   out.write(STRUCT_TYPE);
   out.writeUTF(name);
   out.writeInt(structTypes.size());
   for (InferredType it : structTypes) {
     it.write(out);
   }
 }
  /**
   * Writes a string to the specified DataOutput using <a
   * href="DataInput.html#modified-utf-8">modified UTF-8</a> encoding in a machine-independent
   * manner.
   *
   * <p>First, two bytes are written to out as if by the <code>writeShort</code> method giving the
   * number of bytes to follow. This value is the number of bytes actually written out, not the
   * length of the string. Following the length, each character of the string is output, in
   * sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the
   * counter <code>written</code> is incremented by the total number of bytes written to the output
   * stream. This will be at least two plus the length of <code>str</code>, and at most two plus
   * thrice the length of <code>str</code>.
   *
   * @param str a string to be written.
   * @param out destination to write to
   * @return The number of bytes written out.
   * @exception IOException if an I/O error occurs.
   */
  static int writeUTF(String str, DataOutput out) throws IOException {
    int strlen = str.length();
    int utflen = 0;
    int c, count = 0;

    /* use charAt instead of copying String to char array */
    for (int i = 0; i < strlen; i++) {
      c = str.charAt(i);
      if ((c >= 0x0001) && (c <= 0x007F)) {
        utflen++;
      } else if (c > 0x07FF) {
        utflen += 3;
      } else {
        utflen += 2;
      }
    }

    if (utflen > 65535)
      throw new IllegalArgumentException("encoded string too long: " + utflen + " bytes");

    byte[] bytearr = null;
    if (out instanceof DataOutputStream) {
      DataOutputStream dos = (DataOutputStream) out;
      if (dos.bytearr == null || (dos.bytearr.length < (utflen + 2)))
        dos.bytearr = new byte[(utflen * 2) + 2];
      bytearr = dos.bytearr;
    } else {
      bytearr = new byte[utflen + 2];
    }

    bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
    bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);

    int i = 0;
    for (i = 0; i < strlen; i++) {
      c = str.charAt(i);
      if (!((c >= 0x0001) && (c <= 0x007F))) break;
      bytearr[count++] = (byte) c;
    }

    for (; i < strlen; i++) {
      c = str.charAt(i);
      if ((c >= 0x0001) && (c <= 0x007F)) {
        bytearr[count++] = (byte) c;

      } else if (c > 0x07FF) {
        bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
        bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
        bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
      } else {
        bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
        bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
      }
    }
    out.write(bytearr, 0, utflen + 2);
    return utflen + 2;
  }
  public void write(DataOutput out) throws InvalidByteCodeException, IOException {

    out.writeShort(attributeNameIndex);
    out.writeInt(getAttributeLength());
    if (getClass().equals(AttributeInfo.class)) {
      out.write(info);
      if (debug) debug("wrote " + getDebugMessage());
    }
  }
  public final void write(DataOutput out) throws IOException {
    Text.writeString(out, id); // write url

    out.writeInt(content.length); // write content
    out.write(content);

    Text.writeString(out, contentType); // write contentType

    metadata.write(out); // write metadata
  }
 /*
  *
  * Write a String as a Network Int n, followed by n Bytes
  * Alternative to 16 bit read/writeUTF.
  * Encoding standard is... ?
  *
  */
 public static void writeString(DataOutput out, String s) throws IOException {
   if (s != null) {
     byte[] buffer = s.getBytes("UTF-8");
     int len = buffer.length;
     out.writeInt(len);
     out.write(buffer, 0, len);
   } else {
     out.writeInt(-1);
   }
 }
 @Override
 public void serialize(DataOutput dataOutput, GeobufFeature geobufFeature) throws IOException {
   GeobufEncoder enc = getEncoder();
   // This could be more efficient, we're wrapping a single feature in a feature collection. But
   // that way the key/value
   // serialization all works.
   Geobuf.Data feat = enc.makeFeatureCollection(Arrays.asList(geobufFeature));
   byte[] data = feat.toByteArray();
   dataOutput.writeInt(data.length);
   dataOutput.write(data);
 }
  public void marshal(StompFrame stomp, DataOutput os) throws IOException {
    StringBuilder buffer = new StringBuilder(1024);
    buffer.append(stomp.getAction());
    buffer.append(Stomp.NEWLINE);

    // Output the headers.
    for (Map.Entry<String, String> entry : stomp.getHeaders().entrySet()) {
      buffer.append(entry.getKey());
      buffer.append(Stomp.Headers.SEPERATOR);
      buffer.append(entry.getValue());
      buffer.append(Stomp.NEWLINE);
    }

    // Add a newline to seperate the headers from the content.
    buffer.append(Stomp.NEWLINE);

    os.write(buffer.toString().getBytes("UTF-8"));
    os.write(stomp.getContent());
    os.write(END_OF_FRAME);
  }
 /**
  * {@inheritDoc}
  *
  * @see Writable#write(DataOutput)
  */
 public void write(DataOutput out) throws IOException {
   BSONEncoder enc = new BSONEncoder();
   BasicOutputBuffer buf = new BasicOutputBuffer();
   enc.set(buf);
   enc.putObject(_doc);
   enc.done();
   out.writeInt(buf.size());
   // For better performance we can copy BasicOutputBuffer.pipe(OutputStream)
   // to have a method signature that works with DataOutput
   out.write(buf.toByteArray());
 }
  /**
   * Serialize.
   *
   * <p>The current bitmap is not modified.
   *
   * @param out the DataOutput stream
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void serialize(DataOutput out) throws IOException {
    out.write(SERIAL_COOKIE & 0xFF);
    out.write((SERIAL_COOKIE >>> 8) & 0xFF);
    out.write((SERIAL_COOKIE >>> 16) & 0xFF);
    out.write((SERIAL_COOKIE >>> 24) & 0xFF);

    out.write(this.size & 0xFF);
    out.write((this.size >>> 8) & 0xFF);
    out.write((this.size >>> 16) & 0xFF);
    out.write((this.size >>> 24) & 0xFF);

    for (int k = 0; k < size; ++k) {
      out.write(this.array[k].key & 0xFF);
      out.write((this.array[k].key >>> 8) & 0xFF);
      out.write((this.array[k].value.getCardinality() - 1) & 0xFF);
      out.write(((this.array[k].value.getCardinality() - 1) >>> 8) & 0xFF);
    }
    for (int k = 0; k < size; ++k) {
      array[k].value.writeArray(out);
    }
  }
Exemple #14
0
  /**
   * Streams all members (dest and src addresses, buffer and headers) to the output stream.
   *
   * @param out
   * @throws Exception
   */
  public void writeTo(DataOutput out) throws Exception {
    byte leading = 0;

    if (dest_addr != null) leading = Util.setFlag(leading, DEST_SET);

    if (src_addr != null) leading = Util.setFlag(leading, SRC_SET);

    if (buf != null) leading = Util.setFlag(leading, BUF_SET);

    // 1. write the leading byte first
    out.write(leading);

    // 2. the flags (e.g. OOB, LOW_PRIO), skip the transient flags
    out.writeShort(flags);

    // 3. dest_addr
    if (dest_addr != null) Util.writeAddress(dest_addr, out);

    // 4. src_addr
    if (src_addr != null) Util.writeAddress(src_addr, out);

    // 5. buf
    if (buf != null) {
      out.writeInt(length);
      out.write(buf, offset, length);
    }

    // 6. headers
    int size = headers.size();
    out.writeShort(size);
    final short[] ids = headers.getRawIDs();
    final Header[] hdrs = headers.getRawHeaders();
    for (int i = 0; i < ids.length; i++) {
      if (ids[i] > 0) {
        out.writeShort(ids[i]);
        writeHeader(hdrs[i], out);
      }
    }
  }
 public void write(DataOutput out) throws IOException {
   out.write(BASE_TYPE);
   out.writeUTF(name);
   out.writeInt(sampleStrs.size());
   for (int i = 0; i < sampleStrs.size(); i++) {
     UTF8.writeString(out, sampleStrs.get(i));
   }
   out.writeInt(tokenClassIdentifier);
   out.writeBoolean(tokenParameter != null);
   if (tokenParameter != null) {
     UTF8.writeString(out, tokenParameter);
   }
 }
  public void writeProperties(DataOutput buffer, List<L2Property> list, UnrealPackageReadOnly up)
      throws UnrealException {
    try {
      for (L2Property property : list) {
        Property template = property.getTemplate();

        for (int i = 0; i < property.getSize(); i++) {
          Object obj = property.getAt(i);
          if (obj == null) continue;

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          DataOutput objBuffer = new DataOutputStream(baos, buffer.getCharset());
          AtomicBoolean array = new AtomicBoolean(i > 0);
          AtomicReference<String> structName = new AtomicReference<>();
          AtomicReference<Type> type =
              new AtomicReference<>(
                  Type.valueOf(
                      template.getClass().getSimpleName().replace("Property", "").toUpperCase()));
          write(objBuffer, template, obj, array, structName, type, up);
          byte[] bytes = baos.toByteArray();

          int size = getPropertySize(bytes.length);
          int ord = type.get().ordinal();
          if (ord == 8) // FIXME
          ord = 5;
          int info = (array.get() ? 1 << 7 : 0) | (size << 4) | ord;

          buffer.writeCompactInt(up.nameReference(template.getEntry().getObjectName().getName()));
          buffer.writeByte(info);

          if (type.get() == Type.STRUCT) buffer.writeCompactInt(up.nameReference(structName.get()));
          switch (size) {
            case 5:
              buffer.writeByte(bytes.length);
              break;
            case 6:
              buffer.writeShort(bytes.length);
              break;
            case 7:
              buffer.writeInt(bytes.length);
              break;
          }
          if (i > 0) buffer.writeByte(i);
          buffer.write(bytes);
        }
      }
      buffer.writeCompactInt(up.nameReference("None"));
    } catch (IOException e) {
      throw new UnrealException(e);
    }
  }
Exemple #17
0
 public static int writeCompressedByteArray(DataOutput out, byte[] bytes) throws IOException {
   if (bytes != null) {
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     GZIPOutputStream gzout = new GZIPOutputStream(bos);
     gzout.write(bytes, 0, bytes.length);
     gzout.close();
     byte[] buffer = bos.toByteArray();
     int len = buffer.length;
     out.writeInt(len);
     out.write(buffer, 0, len);
     /* debug only! Once we have confidence, can lose this. */
     return ((bytes.length != 0) ? (100 * buffer.length) / bytes.length : 0);
   } else {
     out.writeInt(-1);
     return -1;
   }
 }
Exemple #18
0
 public void writeTo(DataOutput out) throws Exception {
   out.writeByte(type.ordinal());
   // We can't use Util.writeObject since it's size is limited to 2^15-1
   try {
     if (object instanceof Streamable) {
       out.writeShort(-1);
       Util.writeGenericStreamable((Streamable) object, out);
     } else {
       byte[] bytes = Util.objectToByteBuffer(object);
       out.writeInt(bytes.length);
       out.write(bytes);
     }
   } catch (IOException e) {
     throw e;
   } catch (Exception e) {
     throw new IOException("Exception encountered while serializing execution request", e);
   }
   out.writeLong(request);
 }
 public void write(DataOutput out) throws IOException {
   out.write(ARRAY_TYPE);
   out.writeUTF(name);
   bodyType.write(out);
 }
Exemple #20
0
 public void writeTo(DataOutput out) throws Exception {
   out.writeByte(type);
   out.writeShort(version.length);
   out.write(version);
 }
  @Override
  public void write(DataOutput out) throws IOException {
    int numCol = colType.length;
    boolean[] nullBits = new boolean[numCol];
    int[] colLength = new int[numCol];
    byte[] enumType = new byte[numCol];
    int[] padLength = new int[numCol];
    byte[] padbytes = new byte[8];

    /**
     * Compute the total payload and header length header = total length (4 byte), Version (2 byte),
     * Error (1 byte), #col (2 byte) col type array = #col * 1 byte null bit array = ceil(#col/8)
     */
    int datlen = 4 + 2 + 1 + 2;
    datlen += numCol;
    datlen += getNullByteArraySize(numCol);

    for (int i = 0; i < numCol; i++) {
      /* Get the enum type */
      DBType coldbtype;
      switch (DataType.get(colType[i])) {
        case BIGINT:
          coldbtype = DBType.BIGINT;
          break;
        case BOOLEAN:
          coldbtype = DBType.BOOLEAN;
          break;
        case FLOAT8:
          coldbtype = DBType.FLOAT8;
          break;
        case INTEGER:
          coldbtype = DBType.INTEGER;
          break;
        case REAL:
          coldbtype = DBType.REAL;
          break;
        case SMALLINT:
          coldbtype = DBType.SMALLINT;
          break;
        case BYTEA:
          coldbtype = DBType.BYTEA;
          break;
        default:
          coldbtype = DBType.TEXT;
      }
      enumType[i] = (byte) (coldbtype.ordinal());

      /* Get the actual value, and set the null bit */
      if (colValue[i] == null) {
        nullBits[i] = true;
        colLength[i] = 0;
      } else {
        nullBits[i] = false;

        /*
         * For fixed length type, we get the fixed length.
         * For var len binary format, the length is in the col value.
         * For text format, we must convert encoding first.
         */
        if (!coldbtype.isVarLength()) {
          colLength[i] = coldbtype.getTypeLength();
        } else if (!isTextForm(colType[i])) {
          colLength[i] = ((byte[]) colValue[i]).length;
        } else {
          colLength[i] = ((String) colValue[i]).getBytes(CHARSET).length;
        }

        /* calculate and add the type alignment padding */
        padLength[i] = roundUpAlignment(datlen, coldbtype.getAlignment()) - datlen;
        datlen += padLength[i];

        /* for variable length type, we add a 4 byte length header */
        if (coldbtype.isVarLength()) {
          datlen += 4;
        }
      }
      datlen += colLength[i];
    }

    /*
     * Add the final alignment padding for the next record
     */
    int endpadding = roundUpAlignment(datlen, 8) - datlen;
    datlen += endpadding;

    /* Construct the packet header */
    out.writeInt(datlen);
    out.writeShort(VERSION);
    out.writeByte(errorFlag);
    out.writeShort(numCol);

    /* Write col type */
    for (int i = 0; i < numCol; i++) {
      out.writeByte(enumType[i]);
    }

    /* Nullness */
    byte[] nullBytes = boolArrayToByteArray(nullBits);
    out.write(nullBytes);

    /* Column Value */
    for (int i = 0; i < numCol; i++) {
      if (!nullBits[i]) {
        /* Pad the alignment byte first */
        if (padLength[i] > 0) {
          out.write(padbytes, 0, padLength[i]);
        }

        /* Now, write the actual column value */
        switch (DataType.get(colType[i])) {
          case BIGINT:
            out.writeLong(((Long) colValue[i]));
            break;
          case BOOLEAN:
            out.writeBoolean(((Boolean) colValue[i]));
            break;
          case FLOAT8:
            out.writeDouble(((Double) colValue[i]));
            break;
          case INTEGER:
            out.writeInt(((Integer) colValue[i]));
            break;
          case REAL:
            out.writeFloat(((Float) colValue[i]));
            break;
          case SMALLINT:
            out.writeShort(((Short) colValue[i]));
            break;

            /* For BYTEA format, add 4byte length header at the beginning  */
          case BYTEA:
            out.writeInt(colLength[i]);
            out.write((byte[]) colValue[i]);
            break;

            /* For text format, add 4byte length header. string is already '\0' terminated */
          default:
            {
              out.writeInt(colLength[i]);
              byte[] data = ((String) colValue[i]).getBytes(CHARSET);
              out.write(data);
              break;
            }
        }
      }
    }

    /* End padding */
    out.write(padbytes, 0, endpadding);
  }
Exemple #22
0
 @Override
 public void serialize(DataOutput out, String value) throws IOException {
   byte[] buf = fromHexString(value);
   out.writeInt(buf.length);
   out.write(buf);
 }
Exemple #23
0
 public void serialize(DataOutput out, Object obj) throws IOException {
   byte[] data = (byte[]) obj;
   out.write(data);
 }
Exemple #24
0
  private void writeRecord(DataOutput dataOutput, Object[] objectArray) throws IOException {

    dataOutput.write((byte) ' ');
    for (int j = 0; j < this.header.fieldArray.length; j++) {
      /* iterate throught fields */

      switch (this.header.fieldArray[j].getDataType()) {
        case 'C':
          if (objectArray[j] != null) {

            String str_value = objectArray[j].toString();
            dataOutput.write(
                Utils.textPadding(
                    str_value, characterSetName, this.header.fieldArray[j].getFieldLength()));
          } else {

            dataOutput.write(
                Utils.textPadding(
                    "", this.characterSetName, this.header.fieldArray[j].getFieldLength()));
          }

          break;

        case 'D':
          if (objectArray[j] != null) {

            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime((Date) objectArray[j]);
            dataOutput.write(String.valueOf(calendar.get(Calendar.YEAR)).getBytes());
            dataOutput.write(
                Utils.textPadding(
                    String.valueOf(calendar.get(Calendar.MONTH) + 1),
                    this.characterSetName,
                    2,
                    Utils.ALIGN_RIGHT,
                    (byte) '0'));
            dataOutput.write(
                Utils.textPadding(
                    String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),
                    this.characterSetName,
                    2,
                    Utils.ALIGN_RIGHT,
                    (byte) '0'));
          } else {

            dataOutput.write("        ".getBytes());
          }

          break;

        case 'F':
          if (objectArray[j] != null) {

            dataOutput.write(
                Utils.doubleFormating(
                    (Double) objectArray[j],
                    this.characterSetName,
                    this.header.fieldArray[j].getFieldLength(),
                    this.header.fieldArray[j].getDecimalCount()));
          } else {

            dataOutput.write(
                Utils.textPadding(
                    " ",
                    this.characterSetName,
                    this.header.fieldArray[j].getFieldLength(),
                    Utils.ALIGN_RIGHT));
          }

          break;

        case 'N':
          if (objectArray[j] != null) {

            dataOutput.write(
                Utils.doubleFormating(
                    (Double) objectArray[j],
                    this.characterSetName,
                    this.header.fieldArray[j].getFieldLength(),
                    this.header.fieldArray[j].getDecimalCount()));
          } else {

            dataOutput.write(
                Utils.textPadding(
                    " ",
                    this.characterSetName,
                    this.header.fieldArray[j].getFieldLength(),
                    Utils.ALIGN_RIGHT));
          }

          break;
        case 'L':
          if (objectArray[j] != null) {

            if ((Boolean) objectArray[j] == Boolean.TRUE) {

              dataOutput.write((byte) 'T');
            } else {

              dataOutput.write((byte) 'F');
            }
          } else {

            dataOutput.write((byte) '?');
          }

          break;

        case 'M':
          break;

        default:
          throw new DBFException("Unknown field type " + this.header.fieldArray[j].getDataType());
      }
    } /* iterating through the fields */
  }