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.openxdata.server.serializer.JavaRosaXformSerializer.java

@Override
public void serializeSuccess(OutputStream os) {

    String str = "The data Has been successfully submitted to the server. Cross check with the server admin to clarify";
    DataOutputStream dos = new DataOutputStream(os);
    try {//w w  w .  jav a  2  s.com
        dos.writeUTF(str);
        dos.close();
    } catch (IOException e) {
        throw new UnexpectedException(e);
    }
}

From source file:LCModels.MessageTransmitter.java

@Override
public void run() {
    try {/*from   w  ww. j  a v a 2 s .  c om*/
        // Socket connects to the ServerSocket, ServerSocket receives a Socket, what? haha//
        //send data as points
        //            Socket s = new Socket(hostname,targetPort);
        //            Socket z = new Socket(hostname, 48500, InetAddress.getByName("127.0.0.1"), targetPort);
        Socket client = new Socket(hostname, targetPort);
        /* Get server's OutputStream */
        OutputStream outToServer = client.getOutputStream();
        /* Get server's DataOutputStream to write/send message */
        DataOutputStream out = new DataOutputStream(outToServer);
        /* Write message to DataOutputStream */
        out.writeUTF(transmitJSON.toString());
        /* Get InputStream to get message from server */
        InputStream inFromServer = client.getInputStream();
        /* Get DataInputStream to read message of server */
        DataInputStream in = new DataInputStream(inFromServer);
        /* Print message received from server */
        System.out.println("Server says..." + in.readUTF());
        client.close();
    } catch (IOException ex) {
        Logger.getLogger(MessageTransmitter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openxdata.server.serializer.JavaRosaXformSerializer.java

@Override
public void serializeAccessDenied(OutputStream os) {

    String str = "<HTML><HEAD><TITLE>Data Submission Status</TITLE>"
            + "</HEAD><BODY>Form Submitted Successfully!</BODY></HTML>";
    DataOutputStream dos = new DataOutputStream(os);
    try {//from w  ww.  ja v a  2 s . co m
        dos.writeUTF(str);
        dos.flush();
        dos.close();
    } catch (IOException e) {
        throw new UnexpectedException(e);
    }
}

From source file:org.orbisgis.sqlconsole.api.SQLElement.java

@Override
public void writeStream(DataOutputStream out) throws IOException {
    out.writeUTF(getDocumentPathString());
}

From source file:com.facebook.infrastructure.db.Row.java

public void serialize(Row row, DataOutputStream dos) throws IOException {
    dos.writeUTF(row.key());
    Map<String, ColumnFamily> columnFamilies = row.getColumnFamilyMap();
    int size = columnFamilies.size();
    dos.writeInt(size);//  w w  w  . j  a va 2s  .  c  om

    if (size > 0) {
        Set<String> cNames = columnFamilies.keySet();
        for (String cName : cNames) {
            ColumnFamily.serializer().serialize(columnFamilies.get(cName), dos);
        }
    }
}

From source file:epn.edu.ec.bibliotecadigital.cliente.Client.java

@Override
public void run() {
    try {//from w  w  w  . j  ava2s . c o m
        clientSocketBalancer = new Socket(InetAddress.getByName(serverIP), portBalancer);
        DataInputStream dataInBalancer = new DataInputStream(clientSocketBalancer.getInputStream());
        DataOutputStream dataOut = new DataOutputStream(clientSocketBalancer.getOutputStream());
        dataOut.writeUTF((String) params[0]);//nombre de usuario
        String ipServer = dataInBalancer.readUTF();
        int portServer = dataInBalancer.readInt();
        clientSocketBalancer.close();
        Socket clientSocket = new Socket(ipServer, portServer);
        dataOut = new DataOutputStream(clientSocket.getOutputStream());
        dataOut.writeUTF(accion);
        dataOut.writeUTF((String) params[0]);//nombre de usuario
        InputStream in;
        DataInputStream dataIn;
        switch (accion) {
        case "bajar":

            dataOut = new DataOutputStream(clientSocket.getOutputStream());
            dataOut.writeUTF((String) params[1]);
            dataIn = new DataInputStream(clientSocket.getInputStream());
            boolean encontrado = dataIn.readBoolean();
            if (!encontrado) {
                System.out.println("Libro con el cdigo: " + params[1] + " no encontrado");
                break;
            }
            String fileName = dataIn.readUTF();
            System.out.println(
                    "Descargando libro " + fileName + " con cdigo " + params[1] + " en la carpeta Donwloads");
            String home = System.getProperty("user.home");

            in = clientSocket.getInputStream();
            try {
                FileOutputStream out = new FileOutputStream(new File(home + "\\Downloads\\" + fileName));
                byte[] bytes = new byte[64 * 1024];

                int count;
                while ((count = in.read(bytes)) > 0) {
                    out.write(bytes, 0, count);
                }
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(in);
            }
            break;
        case "subir":
            dataOut = new DataOutputStream(clientSocket.getOutputStream());
            dataOut.writeUTF(((File) params[1]).getName());
            OutputStream out = clientSocket.getOutputStream();
            try {
                byte[] bytes = new byte[64 * 1024];
                in = new FileInputStream((File) params[1]);

                int count;
                while ((count = in.read(bytes)) > 0) {
                    out.write(bytes, 0, count);
                }
                in.close();
            } finally {
                IOUtils.closeQuietly(out);
            }
            break;
        case "obtenerLista":
            ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream());
            System.out.println("Libros disponibles: \n");
            List<Libro> libros = (List<Libro>) inFromClient.readObject();
            System.out.println("\tCdigo\tNommbre\n");
            for (Libro lbr : libros) {
                System.out.println("\t" + lbr.getCodigolibro() + "\t" + lbr.getNombre());
            }
            inFromClient.close();
            break;
        case "verificar":

            break;
        }
        dataOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.apache.cassandra.db.SliceByNamesReadCommand.java

@Override
public void serialize(ReadCommand rm, DataOutputStream dos) throws IOException {
    SliceByNamesReadCommand realRM = (SliceByNamesReadCommand) rm;
    dos.writeBoolean(realRM.isDigestQuery());
    dos.writeUTF(realRM.table);
    dos.writeUTF(realRM.key);// ww  w.j a v  a 2  s.  c o  m
    realRM.columnParent.serialize(dos);
    dos.writeInt(realRM.columnNames.size());
    if (realRM.columnNames.size() > 0) {
        for (byte[] cName : realRM.columnNames) {
            ColumnSerializer.writeName(cName, dos);
        }
    }
}

From source file:J2MEMixedRecordEnumerationExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//from   w  w w .  j a  va 2  s  . com
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "First Record", "Second Record", "Third Record" };
            int outputInteger[] = { 15, 10, 5 };
            boolean outputBoolean[] = { true, false, true };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeBoolean(outputBoolean[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
            }
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
            StringBuffer buffer = new StringBuffer();
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            recordEnumeration = recordstore.enumerateRecords(null, null, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append("\n");
                buffer.append(inputDataStream.readBoolean());
                buffer.append("\n");
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
            inputStream.close();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
                recordEnumeration.destroy();
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }

}

From source file:WriteReadMixedDataTypesExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//from   w w  w.j a  v  a2  s . c o  m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString = "First Record";
            int outputInteger = 15;
            boolean outputBoolean = true;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            outputDataStream.writeUTF(outputString);
            outputDataStream.writeBoolean(outputBoolean);
            outputDataStream.writeInt(outputInteger);
            outputDataStream.flush();
            outputRecord = outputStream.toByteArray();
            recordstore.addRecord(outputRecord, 0, outputRecord.length);
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            String inputString = null;
            int inputInteger = 0;
            boolean inputBoolean = false;
            byte[] byteInputData = new byte[100];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            for (int x = 1; x <= recordstore.getNumRecords(); x++) {
                recordstore.getRecord(x, byteInputData, 0);
                inputString = inputDataStream.readUTF();
                inputBoolean = inputDataStream.readBoolean();
                inputInteger = inputDataStream.readInt();
                inputStream.reset();
            }
            inputStream.close();
            inputDataStream.close();
            alert = new Alert("Reading", inputString + " " + inputInteger + " " + inputBoolean, null,
                    AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:com.csipsimple.backup.SipProfilesHelper.java

private void writeData(BackupDataOutput data, String value) throws IOException {
    // Create buffer stream and data output stream for our data
    ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
    DataOutputStream outWriter = new DataOutputStream(bufStream);
    // Write structured data
    outWriter.writeUTF(value);
    // Send the data to the Backup Manager via the BackupDataOutput
    byte[] buffer = bufStream.toByteArray();
    int len = buffer.length;
    data.writeEntityHeader(ACCOUNTS_BACKUP_KEY, len);
    data.writeEntityData(buffer, len);/* w w  w .  ja va  2 s. c o m*/
}