Example usage for java.io DataOutputStream writeUTF

List of usage examples for java.io DataOutputStream writeUTF

Introduction

In this page you can find the example usage for java.io DataOutputStream writeUTF.

Prototype

public final void writeUTF(String str) throws IOException 

Source Link

Document

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

Usage

From source file:org.nuxeo.osgi.BundleIdGenerator.java

public synchronized void store(File file) throws IOException {
    DataOutputStream out = null;
    try {//from w  w  w .j av a  2  s.  c  o m
        out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        out.writeLong(count);
        int size = ids.size();
        out.writeInt(size);
        for (Map.Entry<String, Long> entry : ids.entrySet()) {
            out.writeUTF(entry.getKey());
            out.writeLong(entry.getValue());
        }
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:Bookmark.java

protected void javaToNative(Object object, TransferData transferData) {
    if (object == null || !(object instanceof Bookmark))
        return;/* w  w w.j  a va  2 s  .co  m*/

    Bookmark bookmark = (Bookmark) object;

    if (isSupportedType(transferData)) {
        try {
            // Writes data to a byte array.
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            out.writeUTF(bookmark.name);
            out.writeUTF(bookmark.href);
            out.writeUTF(bookmark.addDate);
            out.writeUTF(bookmark.lastVisited);
            out.writeUTF(bookmark.lastModified);
            out.close();

            super.javaToNative(stream.toByteArray(), transferData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:io.dacopancm.socketdcm.net.StreamSocket.java

public void handle() {
    try {//  w w w.  j av a 2 s. co  m

        if (ConectType.GET_STREAMLIST.name().equalsIgnoreCase(method)) {
            DataOutputStream dOut = new DataOutputStream(sock.getOutputStream());
            DataInputStream dIn = new DataInputStream(sock.getInputStream());

            String files = HelperUtil.toJSON(mainApp.getStreamFilesList().toArray());

            dOut.writeUTF(files);
            dOut.flush(); // Send off the data
            dOut.close();
        } else if (ConectType.GET_STREAM.name().equalsIgnoreCase(method)) {
            DataOutputStream dOut = new DataOutputStream(sock.getOutputStream());
            DataInputStream dIn = new DataInputStream(sock.getInputStream());

            boolean player = mainApp.playStream(streamid);

            dOut.writeUTF("" + (player ? HelperUtil.VLC_SRV_PORT : player));
            dOut.flush(); // Send off the data
            dOut.close();

        }
    } catch (IOException ex) {
        HelperUtil.showErrorB("No se pudo establecer conexin");
        try {
            if (sock != null) {
                sock.close();
            }
        } catch (IOException e) {
        }
    }
}

From source file:org.darwinathome.server.controller.HubController.java

@RequestMapping(Hub.GET_WORLD_SERVICE)
public void getWorld(@PathVariable("session") String session, OutputStream outputStream) throws IOException {
    log.info(Hub.GET_WORLD_SERVICE + " " + session);
    DataOutputStream dos = new DataOutputStream(outputStream);
    PlayerSession playerSession = sessionMap.get(session);
    if (playerSession != null) {
        dos.writeUTF(Hub.SUCCESS);
        dos.write(worldHistory.getFrozenWorld().getWorld());
        Iterator<PlayerSession> sessionWalk = sessionMap.values().iterator();
        while (sessionWalk.hasNext()) {
            if (sessionWalk.next().isExpired()) {
                sessionWalk.remove();// w  w  w  .j av  a 2s .  c  om
            }
        }
    } else {
        dos.writeUTF(Hub.FAILURE);
        dos.writeUTF(Failure.SESSION.toString());
    }
}

From source file:org.darwinathome.server.controller.HubController.java

@RequestMapping(Hub.GET_SPEECH_SINCE_SERVICE)
public void getSpeechSince(@PathVariable("session") String session, @RequestParam(Hub.PARAM_TIME) long time,
        OutputStream outputStream) throws IOException {
    log.info(Hub.GET_SPEECH_SINCE_SERVICE + " " + session);
    DataOutputStream dos = new DataOutputStream(outputStream);
    try {/*from ww  w .  j ava 2  s  . c  o  m*/
        List<SpeechChange> changes = worldHistory.getSpeechSince(getEmail(session), time);
        dos.writeUTF(Hub.SUCCESS);
        SpeechChange.write(dos, changes);
    } catch (NoSessionException e) {
        dos.writeUTF(Hub.FAILURE);
        dos.writeUTF(Failure.SESSION.toString());
    }
}

From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java

@Override
public <K> boolean containsKey(final K key, final Serializer<K> keySerializer) throws IOException {
    return withCommsSession(new CommsAction<Boolean>() {
        @Override// w w  w.  j  av  a 2  s  . c  o m
        public Boolean execute(final CommsSession session) throws IOException {
            final DataOutputStream dos = new DataOutputStream(session.getOutputStream());
            dos.writeUTF("containsKey");

            serialize(key, keySerializer, dos);
            dos.flush();

            final DataInputStream dis = new DataInputStream(session.getInputStream());
            return dis.readBoolean();
        }
    });
}

From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java

@Override
public <K, V> boolean replace(final K key, final V value, final Serializer<K> keySerializer,
        final Serializer<V> valueSerializer, final long revision) throws IOException {
    return withCommsSession(session -> {
        validateProtocolVersion(session, 2);

        final DataOutputStream dos = new DataOutputStream(session.getOutputStream());
        dos.writeUTF("replace");

        serialize(key, keySerializer, dos);
        dos.writeLong(revision);//  w ww  .j a va 2s. c  o m
        serialize(value, valueSerializer, dos);

        dos.flush();

        // read response
        final DataInputStream dis = new DataInputStream(session.getInputStream());
        return dis.readBoolean();
    });
}

From source file:IntSort.java

public void writeStream(String[] sData, boolean[] bData, int[] iData) {
    try {/*ww w.  j a  v a  2  s  .  c  om*/
        // Write data into an internal byte array
        ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();

        // Write Java data types into the above byte array
        DataOutputStream strmDataType = new DataOutputStream(strmBytes);

        byte[] record;

        for (int i = 0; i < sData.length; i++) {
            // Write Java data types      
            strmDataType.writeUTF(sData[i]);
            strmDataType.writeBoolean(bData[i]);
            strmDataType.writeInt(iData[i]);

            // Clear any buffered data
            strmDataType.flush();

            // Get stream data into byte array and write record      
            record = strmBytes.toByteArray();
            rs.addRecord(record, 0, record.length);

            // Toss any data in the internal array so writes 
            // starts at beginning (of the internal array)
            strmBytes.reset();
        }

        strmBytes.close();
        strmDataType.close();

    } catch (Exception e) {
        db(e.toString());
    }
}

From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java

@Override
public <K> boolean remove(final K key, final Serializer<K> serializer) throws IOException {
    return withCommsSession(new CommsAction<Boolean>() {
        @Override//ww w  . ja v a2 s  .co  m
        public Boolean execute(final CommsSession session) throws IOException {
            final DataOutputStream dos = new DataOutputStream(session.getOutputStream());
            dos.writeUTF("remove");

            serialize(key, serializer, dos);
            dos.flush();

            // read response
            final DataInputStream dis = new DataInputStream(session.getInputStream());
            return dis.readBoolean();
        }
    });
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.PatientSerializer.java

/**
 * Write the patient information to the output stream.
 *
 * @param stream the output stream/*from   w ww  . j  a va2s . com*/
 * @param data   the data that need to be written to the output stream
 */
@Override
public void write(final OutputStream stream, final Object data) throws IOException {
    try {
        Patient patient = (Patient) data;

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

        DataOutputStream outputStream = new DataOutputStream(stream);
        // skip if the patient is an invalid patient
        if (patient == null || patient.getPersonName() == null || patient.getPatientIdentifier() == null)
            return;

        outputStream.writeInt(patient.getPatientId());

        PersonName personName = patient.getPersonName();
        outputStream.writeUTF(StringUtils.defaultString(personName.getFamilyName()));
        outputStream.writeUTF(StringUtils.defaultString(personName.getMiddleName()));
        outputStream.writeUTF(StringUtils.defaultString(personName.getGivenName()));

        outputStream.writeUTF(StringUtils.defaultString(patient.getGender()));

        Date birthDate = patient.getBirthdate();
        outputStream.writeUTF(birthDate != null ? dateFormat.format(birthDate.getTime()) : StringUtils.EMPTY);

        PatientIdentifier patientIdentifier = patient.getPatientIdentifier();
        outputStream.writeUTF(StringUtils.defaultString(patientIdentifier.getIdentifier()));
    } catch (IOException e) {
        log.info("Writing patient information failed!", e);
    }
}