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

The following examples show how to use java.io.DataOutputStream#write() . 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: TestTFile.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private int writePrepWithKnownLength(Writer writer, int start, int n)
    throws IOException {
  // get the length of the key
  String key = String.format(localFormatter, start);
  int keyLen = key.getBytes().length;
  String value = "value" + key;
  int valueLen = value.getBytes().length;
  for (int i = start; i < (start + n); i++) {
    DataOutputStream out = writer.prepareAppendKey(keyLen);
    String localKey = String.format(localFormatter, i);
    out.write(localKey.getBytes());
    out.close();
    out = writer.prepareAppendValue(valueLen);
    String localValue = "value" + localKey;
    out.write(localValue.getBytes());
    out.close();
  }
  return (start + n);
}
 
Example 2
Source File: FragmentsResponse.java    From pxf with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes a fragments list in JSON, To be used as the result string for
 * GPDB. An example result is as follows:
 * <code>{"PXFFragments":[{"replicas":
 * ["sdw1.corp.emc.com","sdw3.corp.emc.com","sdw8.corp.emc.com"],
 * "sourceName":"text2.csv", "index":"0","metadata":"&lt;base64 metadata for fragment&gt;",
 * "userData":"&lt;data_specific_to_third_party_fragmenter&gt;"
 * },{"replicas":["sdw2.corp.emc.com","sdw4.corp.emc.com","sdw5.corp.emc.com"
 * ],"sourceName":"text_data.csv","index":"0","metadata":
 * "&lt;base64 metadata for fragment&gt;"
 * ,"userData":"&lt;data_specific_to_third_party_fragmenter&gt;"
 * }]}</code>
 */
@Override
public void write(OutputStream output) throws IOException,
        WebApplicationException {
    DataOutputStream dos = new DataOutputStream(output);
    ObjectMapper mapper = new ObjectMapper();

    dos.write("{\"PXFFragments\":[".getBytes());

    String prefix = "";
    for (Fragment fragment : fragments) {
        StringBuilder result = new StringBuilder();
        /* metaData and userData are automatically converted to Base64 */
        result.append(prefix).append(mapper.writeValueAsString(fragment));
        prefix = ",";
        dos.write(result.toString().getBytes());
    }

    dos.write("]}".getBytes());
}
 
Example 3
Source File: BinaryAttribute.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static void write(BinaryAttribute attributes, DataOutputStream out,
                  BinaryConstantPool cpool, Environment env) throws IOException {
    // count the number of attributes
    int attributeCount = 0;
    for (BinaryAttribute att = attributes; att != null; att = att.next)
        attributeCount++;
    out.writeShort(attributeCount);

    // write out each attribute
    for (BinaryAttribute att = attributes; att != null; att = att.next) {
        Identifier name = att.name;
        byte data[] = att.data;
        // write the identifier
        out.writeShort(cpool.indexString(name.toString(), env));
        // write the length
        out.writeInt(data.length);
        // write the data
        out.write(data, 0, data.length);
    }
}
 
Example 4
Source File: TestTFile.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private int writePrepWithKnownLength(Writer writer, int start, int n)
    throws IOException {
  // get the length of the key
  String key = String.format(localFormatter, start);
  int keyLen = key.getBytes().length;
  String value = "value" + key;
  int valueLen = value.getBytes().length;
  for (int i = start; i < (start + n); i++) {
    DataOutputStream out = writer.prepareAppendKey(keyLen);
    String localKey = String.format(localFormatter, i);
    out.write(localKey.getBytes());
    out.close();
    out = writer.prepareAppendValue(valueLen);
    String localValue = "value" + localKey;
    out.write(localValue.getBytes());
    out.close();
  }
  return (start + n);
}
 
Example 5
Source File: ClassDefinition.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public static void writeTo(ClassDefinition definition, DataOutputStream out) {
    try {
        int code = definition.getCode();
        Specification specification = definition.getSpecification();
        String name = definition.getName();
        byte[] bytes = name.getBytes(StringUtility.CHARSET);
        out.writeShort((short) code);
        out.writeByte(getCode(specification));
        out.writeShort((short) bytes.length);
        out.write(bytes);
        out.writeShort((short) definition.properties.length);
        for (PropertyDefinition property : definition.properties) {
            bytes = property.getName().getBytes(StringUtility.CHARSET);
            out.writeShort((short) property.getCode());
            out.writeShort((short) bytes.length);
            out.write(bytes);
        }
    } catch (Exception exception) {
        throw new CodecDefinitionException(exception);
    }
}
 
Example 6
Source File: BcKeyStoreSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
private void encodeCertificate(
    Certificate         cert,
    DataOutputStream    dOut)
    throws IOException
{
    try
    {
        byte[]      cEnc = cert.getEncoded();

        dOut.writeUTF(cert.getType());
        dOut.writeInt(cEnc.length);
        dOut.write(cEnc);
    }
    catch (CertificateEncodingException ex)
    {
        throw new IOException(ex.toString());
    }
}
 
Example 7
Source File: DbTest.java    From medic-gateway with GNU Affero General Public License v3.0 5 votes vote down vote up
private static SmsMessage aMultipartSmsWith(String from, String content, int referenceNumber, int partNumber, int totalParts) throws IOException {
	SmsMessage m = mock(SmsMessage.class);
	when(m.getMessageBody()).thenReturn(content);
	when(m.getOriginatingAddress()).thenReturn(from);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	UDH: {
		DataOutputStream pdu = new DataOutputStream(baos);

		pdu.write(0);               // empty SMSC number
		pdu.write(1 << 6);          // byte0, including UDH-present flag
		pdu.write(0); pdu.write(0); // an empty FROM number, but min length is 1
		pdu.write(0); pdu.write(0); // PID and DCS (both ignored in our SmsUdh)
		pdu.write(0); pdu.write(0); // \
		pdu.write(0); pdu.write(0); // |- timestamp (ignored)
		pdu.write(0); pdu.write(0); // |
		pdu.write(0);               // /
		pdu.write(0);               // UD Length byte (ignored in our SmsUdh
		pdu.write(5);               // number of bytes following this one...
		pdu.write(0);               // element type id: 8-bit concat info
		pdu.write(5);               // element content length (including this and the previous byte)
		pdu.write(referenceNumber);
		pdu.write(totalParts);
		pdu.write(partNumber);
	}
	when(m.getPdu()).thenReturn(baos.toByteArray());

	return m;
}
 
Example 8
Source File: MessageSerializerV3.java    From galaxy-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Message message, DataOutputStream dataOutputStream) throws IOException {
  // write version number;
  MessageSerializer.writeMessageVersion(MessageVersion.V3, dataOutputStream);

  // write message;
  byte[] messageData = Utils.serialize(message);
  dataOutputStream.writeInt(messageData.length);
  dataOutputStream.write(messageData);
}
 
Example 9
Source File: DummyRecordSerde.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void writeExternalFileReference(final File externalFile, final DataOutputStream out) throws IOException {
    out.write(EXTERNAL_FILE_INDICATOR);
    out.writeUTF(externalFile.getAbsolutePath());

    externalFilesWritten.add(externalFile);
}
 
Example 10
Source File: CloverDataParser35Test.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected byte[] getBytes() {
	if (bytes == null) {
		try {
			DataRecordMetadata metadata = getMetadata();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream os = new DataOutputStream(baos);
			os.writeLong(CloverDataFormatter.CLOVER_DATA_HEADER);
			os.writeLong(CloverDataFormatter.CLOVER_DATA_COMPATIBILITY_HASH_3_5);
			os.writeByte(3);
			os.writeByte(5);
			os.writeByte(0);
			byte[] extraBytes = new byte[CloverDataFormatter.HEADER_OPTIONS_ARRAY_SIZE_3_5];
			os.write(extraBytes);
			CloverBuffer buffer = CloverBuffer.wrap(new byte[100]);
			metadata.serialize(buffer);
			buffer.flip();
			os.write(buffer.array(), 0, buffer.limit());
			DataRecord record = DataRecordFactory.newRecord(metadata);
			record.getField(0).setValue("test1");
			writeRecord(os, serializeRecord(record, buffer));
			record.getField(0).setValue("test2");
			writeRecord(os, serializeRecord(record, buffer));
			bytes = baos.toByteArray();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	
	return bytes;
}
 
Example 11
Source File: RemoteResourceFactory.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends ServerProtocol> T receiveServerProtocolNegotiation(final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException {
    final String protocolName = dis.readUTF();
    final int version = dis.readInt();

    final T protocol = (T) RemoteResourceManager.createServerProtocol(protocolName);
    final VersionNegotiator negotiator = protocol.getVersionNegotiator();
    if (negotiator.isVersionSupported(version)) {
        dos.write(RESOURCE_OK);
        dos.flush();

        negotiator.setVersion(version);
        return protocol;
    } else {
        final Integer preferred = negotiator.getPreferredVersion(version);
        if (preferred == null) {
            dos.write(ABORT);
            dos.flush();
            throw new HandshakeException("Unable to negotiate an acceptable version of the ServerProtocol " + protocolName);
        }
        dos.write(DIFFERENT_RESOURCE_VERSION);
        dos.writeInt(preferred);
        dos.flush();

        return receiveServerProtocolNegotiation(dis, dos);
    }
}
 
Example 12
Source File: PacketUtils.java    From HolographicDisplays with GNU General Public License v3.0 5 votes vote down vote up
public static void writeString(final DataOutputStream out, final String s, final Charset charset) throws IOException {
	if (charset == PacketUtils.UTF8) {
		writeVarInt(out, s.length());
	} else {
		out.writeShort(s.length());
	}
	out.write(s.getBytes(charset));
}
 
Example 13
Source File: TestTFile.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void writeNumMetablocks(Writer writer, String compression, int n)
    throws IOException {
  for (int i = 0; i < n; i++) {
    DataOutputStream dout =
        writer.prepareMetaBlock("TfileMeta" + i, compression);
    byte[] b = ("something to test" + i).getBytes();
    dout.write(b);
    dout.close();
  }
}
 
Example 14
Source File: SetStorageGroupPlan.java    From incubator-iotdb with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DataOutputStream stream) throws IOException {
  stream.write((byte) PhysicalPlanType.SET_STORAGE_GROUP.ordinal());
  byte[] fullPathBytes = path.getFullPath().getBytes();
  stream.writeInt(fullPathBytes.length);
  stream.write(fullPathBytes);
}
 
Example 15
Source File: Share.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
static void writeMap(DataOutputStream sout, Map<Byte, byte[]> map) throws IOException {
    sout.writeInt(map.size());
    for (Map.Entry<Byte, byte[]> e : map.entrySet()) {
        Byte key = e.getKey();
        byte[] value = e.getValue();

        sout.writeByte(key);
        sout.writeInt(value.length);
        sout.write(value);
    }
}
 
Example 16
Source File: TrieDictionaryBuilder.java    From kylin with Apache License 2.0 4 votes vote down vote up
protected byte[] buildTrieBytes(int baseId) {
    checkOverflowParts(this.root);

    Stats stats = stats();
    int sizeNoValuesBeneath = stats.mbpn_sizeNoValueBeneath;
    int sizeChildOffset = stats.mbpn_sizeChildOffset;

    if (stats.mbpn_footprint <= 0) // must never happen, but let us be cautious
        throw new IllegalStateException("Too big dictionary, dictionary cannot be bigger than 2GB");
    if (stats.mbpn_footprint > _2GB)
        throw new RuntimeException("Too big dictionary, dictionary cannot be bigger than 2GB");

    // write head
    byte[] head;
    try {
        ByteArrayOutputStream byteBuf = new ByteArrayOutputStream();
        DataOutputStream headOut = new DataOutputStream(byteBuf);
        headOut.write(TrieDictionary.MAGIC);
        headOut.writeShort(0); // head size, will back fill
        headOut.writeInt((int) stats.mbpn_footprint); // body size
        headOut.write(sizeChildOffset);
        headOut.write(sizeNoValuesBeneath);
        positiveShortPreCheck(baseId, "baseId");
        headOut.writeShort(baseId);
        positiveShortPreCheck(stats.maxValueLength, "stats.maxValueLength");
        headOut.writeShort(stats.maxValueLength);
        headOut.writeUTF(bytesConverter == null ? "" : bytesConverter.getClass().getName());
        headOut.close();
        head = byteBuf.toByteArray();
        BytesUtil.writeUnsigned(head.length, head, TrieDictionary.MAGIC_SIZE_I, 2);
    } catch (IOException e) {
        throw new RuntimeException(e); // shall not happen, as we are writing in memory
    }

    byte[] trieBytes = new byte[(int) stats.mbpn_footprint + head.length];
    System.arraycopy(head, 0, trieBytes, 0, head.length);

    LinkedList<Node> open = new LinkedList<Node>();
    IdentityHashMap<Node, Integer> offsetMap = new IdentityHashMap<Node, Integer>();

    // write body
    int o = head.length;
    offsetMap.put(root, o);
    o = build_writeNode(root, o, true, sizeNoValuesBeneath, sizeChildOffset, trieBytes);
    if (root.children.isEmpty() == false)
        open.addLast(root);

    while (open.isEmpty() == false) {
        Node parent = open.removeFirst();
        build_overwriteChildOffset(offsetMap.get(parent), o - head.length, sizeChildOffset, trieBytes);
        for (int i = 0; i < parent.children.size(); i++) {
            Node c = parent.children.get(i);
            boolean isLastChild = (i == parent.children.size() - 1);
            offsetMap.put(c, o);
            o = build_writeNode(c, o, isLastChild, sizeNoValuesBeneath, sizeChildOffset, trieBytes);
            if (c.children.isEmpty() == false)
                open.addLast(c);
        }
    }

    if (o != trieBytes.length)
        throw new RuntimeException();
    return trieBytes;
}
 
Example 17
Source File: StampsTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testWriteToCacheWithError() {
    Stamps s = Stamps.getModulesJARs();
    
    assertNull(s.asByteBuffer("mycache.dat"));
    assertNull(s.asStream("mycache.dat"));

    class Up implements Stamps.Updater {
        boolean called;

        public void flushCaches(DataOutputStream os) throws IOException {
            for (int i = 0; i < 1024 * 1024; i++) {
                os.write(10);
            }
            os.flush();
            os.close();
            throw new IOException("Not supported yet.");
        }

        public void cacheReady() {
            called = true;
        }
        
    }
    Up updater = new Up();
    
    s.scheduleSave(updater, "mycache.dat", false);
    
    assertNull(s.asByteBuffer("mycache.dat"));
    assertNull(s.asStream("mycache.dat"));
    
    CharSequence log = Log.enable("org.netbeans", Level.WARNING);
    s.waitFor(false);
    
    assertNull(s.asByteBuffer("mycache.dat"));
    assertNull(s.asStream("mycache.dat"));
    
    if (log.length() < 10) {
        fail("There should be a warning written to log:\n" + log);
    }
    
    assertFalse("cache ready not called", updater.called);
}
 
Example 18
Source File: HurlStack.java    From volley_demo with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection,
        Request<?> request) throws IOException, AuthFailureError {
    switch (request.getMethod()) {
        case Method.DEPRECATED_GET_OR_POST:
            // This is the deprecated way that needs to be handled for backwards compatibility.
            // If the request's post body is null, then the assumption is that the request is
            // GET.  Otherwise, it is assumed that the request is a POST.
            byte[] postBody = request.getPostBody();
            if (postBody != null) {
                // Prepare output. There is no need to set Content-Length explicitly,
                // since this is handled by HttpURLConnection using the size of the prepared
                // output stream.
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.addRequestProperty(HEADER_CONTENT_TYPE,
                        request.getPostBodyContentType());
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                out.write(postBody);
                out.close();
            }
            break;
        case Method.GET:
            // Not necessary to set the request method because connection defaults to GET but
            // being explicit here.
            connection.setRequestMethod("GET");
            break;
        case Method.DELETE:
            connection.setRequestMethod("DELETE");
            break;
        case Method.POST:
            connection.setRequestMethod("POST");
            addBodyIfExists(connection, request);
            break;
        case Method.PUT:
            connection.setRequestMethod("PUT");
            addBodyIfExists(connection, request);
            break;
        case Method.HEAD:
            connection.setRequestMethod("HEAD");
            break;
        case Method.OPTIONS:
            connection.setRequestMethod("OPTIONS");
            break;
        case Method.TRACE:
            connection.setRequestMethod("TRACE");
            break;
        case Method.PATCH:
            connection.setRequestMethod("PATCH");
            addBodyIfExists(connection, request);
            break;
        default:
            throw new IllegalStateException("Unknown method type.");
    }
}
 
Example 19
Source File: Utilities.java    From T0rlib4Android with Apache License 2.0 4 votes vote down vote up
public static Socket socks5rawSocketConnection(String networkHost, int networkPort, String socksHost, int socksPort)
        throws IOException {

    int bytesRead = 0;
    boolean end = false;
    String messageString = "";
    final byte[] messageByte = new byte[1000];

    Socket socket = new Socket();
    socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
    SocketAddress socksAddress = new InetSocketAddress(socksHost, socksPort);
    socket.connect(socksAddress, CONNECT_TIMEOUT_MILLISECONDS);

    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    outputStream.write((byte) 0x05);
    outputStream.write((byte) 0x01);
    outputStream.write((byte) 0x00);
    outputStream.write((byte) 0x01);
    outputStream.write(networkHost.getBytes());
    outputStream.writeShort((short) networkPort);

    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
    messageByte[0] = inputStream.readByte();
    messageByte[1] = inputStream.readByte();
    if (messageByte[0] != (byte) 0x05 || messageByte[1] != (byte) 0x00) {
        socket.close();
        throw new IOException("SOCKS4a connect failed, got " + messageByte[0] + " - " + messageByte[1] +
                ", but expected 0x00 - 0x5a");
    }

    ByteBuffer byteBuffer = ByteBuffer.wrap(messageByte, 0, 2);

    int bytesToRead = byteBuffer.getShort();
    LOG.info("About to read " + bytesToRead + " octets");

    while (!end) {
        bytesRead = inputStream.read(messageByte);
        messageString += new String(messageByte, 0, bytesRead);
        if (messageString.length() == bytesToRead) {
            end = true;
        }
    }

    return socket;

}
 
Example 20
Source File: FastDiffDeltaEncoder.java    From hbase with Apache License 2.0 4 votes vote down vote up
private int compressSingleKeyValue(DataOutputStream out, Cell cell, Cell prevCell)
    throws IOException {
  int flag = 0; // Do not use more bits than will fit into a byte
  int kLength = KeyValueUtil.keyLength(cell);
  int vLength = cell.getValueLength();

  if (prevCell == null) {
    // copy the key, there is no common prefix with none
    out.write(flag);
    ByteBufferUtils.putCompressedInt(out, kLength);
    ByteBufferUtils.putCompressedInt(out, vLength);
    ByteBufferUtils.putCompressedInt(out, 0);
    PrivateCellUtil.writeFlatKey(cell, (DataOutput)out);
    // Write the value part
    PrivateCellUtil.writeValue(out, cell, cell.getValueLength());
  } else {
    int preKeyLength = KeyValueUtil.keyLength(prevCell);
    int preValLength = prevCell.getValueLength();
    // find a common prefix and skip it
    int commonPrefix = PrivateCellUtil.findCommonPrefixInFlatKey(cell, prevCell, true, false);

    if (kLength == preKeyLength) {
      flag |= FLAG_SAME_KEY_LENGTH;
    }
    if (vLength == prevCell.getValueLength()) {
      flag |= FLAG_SAME_VALUE_LENGTH;
    }
    if (cell.getTypeByte() == prevCell.getTypeByte()) {
      flag |= FLAG_SAME_TYPE;
    }

    byte[] curTsBuf = Bytes.toBytes(cell.getTimestamp());
    int commonTimestampPrefix = findCommonTimestampPrefix(curTsBuf,
        Bytes.toBytes(prevCell.getTimestamp()));

    flag |= commonTimestampPrefix << SHIFT_TIMESTAMP_LENGTH;

    // Check if current and previous values are the same. Compare value
    // length first as an optimization.
    if (vLength == preValLength
        && PrivateCellUtil.matchingValue(cell, prevCell, vLength, preValLength)) {
      flag |= FLAG_SAME_VALUE;
    }

    out.write(flag);
    if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
      ByteBufferUtils.putCompressedInt(out, kLength);
    }
    if ((flag & FLAG_SAME_VALUE_LENGTH) == 0) {
      ByteBufferUtils.putCompressedInt(out, vLength);
    }
    ByteBufferUtils.putCompressedInt(out, commonPrefix);
    short rLen = cell.getRowLength();
    if (commonPrefix < rLen + KeyValue.ROW_LENGTH_SIZE) {
      // Previous and current rows are different. Copy the differing part of
      // the row, skip the column family, and copy the qualifier.
      PrivateCellUtil.writeRowKeyExcludingCommon(cell, rLen, commonPrefix, out);
      PrivateCellUtil.writeQualifier(out, cell, cell.getQualifierLength());
    } else {
      // The common part includes the whole row. As the column family is the
      // same across the whole file, it will automatically be included in the
      // common prefix, so we need not special-case it here.
      // What we write here is the non common part of the qualifier
      int commonQualPrefix = commonPrefix - (rLen + KeyValue.ROW_LENGTH_SIZE)
          - (cell.getFamilyLength() + KeyValue.FAMILY_LENGTH_SIZE);
      PrivateCellUtil.writeQualifierSkippingBytes(out, cell, cell.getQualifierLength(),
        commonQualPrefix);
    }
    // Write non common ts part
    out.write(curTsBuf, commonTimestampPrefix, KeyValue.TIMESTAMP_SIZE - commonTimestampPrefix);

    // Write the type if it is not the same as before.
    if ((flag & FLAG_SAME_TYPE) == 0) {
      out.write(cell.getTypeByte());
    }

    // Write the value if it is not the same as before.
    if ((flag & FLAG_SAME_VALUE) == 0) {
      PrivateCellUtil.writeValue(out, cell, vLength);
    }
  }
  return kLength + vLength + KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE;
}