List of usage examples for java.io DataInputStream readUTF
public final String readUTF() throws IOException
readUTF
method of DataInput
. From source file:org.openmrs.module.odkconnector.serialization.web.CohortWebConnectorTest.java
@Test public void serialize_shouldDisplayAllCohortInformation() throws Exception { // compose url URL u = new URL(SERVER_URL + "/module/odkconnector/download/cohort.form"); // setup http url connection HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoOutput(true);/* ww w .j ava 2 s .c o m*/ connection.setRequestMethod("POST"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.addRequestProperty("Content-type", "application/octet-stream"); // write auth details to connection DataOutputStream outputStream = new DataOutputStream(new GZIPOutputStream(connection.getOutputStream())); outputStream.writeUTF("admin"); outputStream.writeUTF("test"); outputStream.writeBoolean(false); outputStream.close(); DataInputStream inputStream = new DataInputStream(new GZIPInputStream(connection.getInputStream())); Integer responseStatus = inputStream.readInt(); if (responseStatus == HttpURLConnection.HTTP_OK) { Integer cohortCounts = inputStream.readInt(); System.out.println("Number of cohorts: " + cohortCounts); for (int i = 0; i < cohortCounts; i++) { System.out.println("Cohort ID: " + inputStream.readInt()); System.out.println("Cohort Name: " + inputStream.readUTF()); } } inputStream.close(); }
From source file:J2MESearchMixedRecordDataTypeExample.java
public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true);/*from w w w .j ava2 s .c o m*/ notifyDestroyed(); } else if (command == start) { try { recordstore = RecordStore.openRecordStore("myRecordStore", true); byte[] outputRecord; String outputString[] = { "A", "B", "M" }; int outputInteger[] = { 15, 10, 5 }; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for (int x = 0; x < 3; x++) { outputDataStream.writeUTF(outputString[x]); outputDataStream.writeInt(outputInteger[x]); outputDataStream.flush(); outputRecord = outputStream.toByteArray(); recordstore.addRecord(outputRecord, 0, outputRecord.length); outputStream.reset(); } outputStream.close(); outputDataStream.close(); String inputString; byte[] byteInputData = new byte[300]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); if (recordstore.getNumRecords() > 0) { filter = new Filter("Mary"); recordEnumeration = recordstore.enumerateRecords(filter, null, false); while (recordEnumeration.hasNextElement()) { recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); inputString = inputDataStream.readUTF() + " " + inputDataStream.readInt(); alert = new Alert("Reading", inputString, null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } inputStream.close(); recordstore.closeRecordStore(); if (RecordStore.listRecordStores() != null) { RecordStore.deleteRecordStore("myRecordStore"); filter.filterClose(); 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:J2MESortMixedRecordDataTypeExample.java
public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true);//from w w w.j a va 2 s . c om notifyDestroyed(); } else if (command == start) { try { recordstore = RecordStore.openRecordStore("myRecordStore", true); byte[] outputRecord; String outputString[] = { "Mary", "Bob", "Adam" }; int outputInteger[] = { 15, 10, 5 }; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for (int x = 0; x < 3; x++) { outputDataStream.writeUTF(outputString[x]); outputDataStream.writeInt(outputInteger[x]); outputDataStream.flush(); outputRecord = outputStream.toByteArray(); recordstore.addRecord(outputRecord, 0, outputRecord.length); outputStream.reset(); } outputStream.close(); outputDataStream.close(); String[] inputString = new String[3]; byte[] byteInputData = new byte[300]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); StringBuffer buffer = new StringBuffer(); comparator = new Comparator(); recordEnumeration = recordstore.enumerateRecords(null, comparator, false); while (recordEnumeration.hasNextElement()) { recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); buffer.append(inputDataStream.readUTF()); buffer.append(inputDataStream.readInt()); buffer.append("\n"); inputDataStream.reset(); } alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); inputDataStream.close(); inputStream.close(); recordstore.closeRecordStore(); if (RecordStore.listRecordStores() != null) { RecordStore.deleteRecordStore("myRecordStore"); comparator.compareClose(); 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:io.dacopancm.socketdcm.net.StreamSocket.java
public ObservableList<StreamFile> getStreamList() { ObservableList<StreamFile> list = FXCollections.observableArrayList(); try {// w ww .j av a2 s . co m if (ConectType.GET_STREAMLIST.name().equalsIgnoreCase(method)) { logger.log(Level.INFO, "get stream list call"); sock = new Socket(ip, port); DataOutputStream dOut = new DataOutputStream(sock.getOutputStream()); DataInputStream dIn = new DataInputStream(sock.getInputStream()); dOut.writeUTF(ConectType.STREAM.name());//send stream type dOut.writeUTF("uncompressed");//send comprimido o no dOut.writeUTF(ConectType.GET_STREAMLIST.toString()); //send type dOut.writeUTF("mayra"); dOut.flush(); // Send off the data System.out.println("Request list send"); String resp = dIn.readUTF(); dOut.close(); logger.log(Level.INFO, "resp get streamlist: {0}", resp); List<StreamFile> files = HelperUtil.fromJSON(resp); list.addAll(files); } } catch (IOException ex) { HelperUtil.showErrorB("No se pudo establecer conexin"); logger.log(Level.INFO, "get streamlist error no connect:{0}", ex.getMessage()); try { if (sock != null) { sock.close(); } } catch (IOException e) { } } return list; }
From source file:org.openmrs.module.odkconnector.serialization.serializer.ListSerializerTest.java
@Test public void serialize_shouldSerializePatientInformation() throws Exception { File file = File.createTempFile("PatientSerialization", "Example"); GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file))); log.info("Writing to: " + file.getAbsolutePath()); Cohort cohort = new Cohort(); cohort.addMember(6);// www . ja v a 2 s . co m cohort.addMember(7); cohort.addMember(8); List<Patient> patients = new ArrayList<Patient>(); List<Obs> observations = new ArrayList<Obs>(); List<Form> forms = new ArrayList<Form>(); for (Integer patientId : cohort.getMemberIds()) { Patient patient = Context.getPatientService().getPatient(patientId); observations.addAll(Context.getObsService().getObservationsByPerson(patient)); patients.add(patient); } Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class); serializer.write(outputStream, patients); serializer.write(outputStream, observations); serializer.write(outputStream, forms); outputStream.close(); GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))); DataInputStream dataInputStream = new DataInputStream(inputStream); // total number of patients Integer patientCounter = dataInputStream.readInt(); System.out.println("Patient Counter: " + patientCounter); for (int i = 0; i < patientCounter; i++) { System.out.println("=================Patient====================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Family Name: " + dataInputStream.readUTF()); System.out.println("Middle Name: " + dataInputStream.readUTF()); System.out.println("Last Name: " + dataInputStream.readUTF()); System.out.println("Gender: " + dataInputStream.readUTF()); System.out.println("Birth Date: " + dataInputStream.readLong()); System.out.println("Identifier" + dataInputStream.readUTF()); } Integer obsCounter = dataInputStream.readInt(); for (int j = 0; j < obsCounter; j++) { System.out.println("==================Observation================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Concept Name: " + dataInputStream.readUTF()); byte type = dataInputStream.readByte(); if (type == ObsSerializer.TYPE_STRING) System.out.println("Value: " + dataInputStream.readUTF()); else if (type == ObsSerializer.TYPE_INT) System.out.println("Value: " + dataInputStream.readInt()); else if (type == ObsSerializer.TYPE_DOUBLE) System.out.println("Value: " + dataInputStream.readDouble()); else if (type == ObsSerializer.TYPE_DATE) System.out.println("Value: " + dataInputStream.readLong()); System.out.println("Time: " + dataInputStream.readLong()); } Integer formCounter = dataInputStream.readInt(); for (int j = 0; j < formCounter; j++) { System.out.println("==================Form================="); System.out.println("Form Id: " + dataInputStream.readInt()); } System.out.println(); inputStream.close(); }
From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.PatientSerializerTest.java
@Test public void serialize_shouldSerializePatientInformation() throws Exception { File file = File.createTempFile("PatientSerialization", "Example"); GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file))); log.info("Writing to: " + file.getAbsolutePath()); Cohort cohort = new Cohort(); cohort.addMember(6);//from w w w . j ava2 s .c o m cohort.addMember(7); cohort.addMember(8); List<Patient> patients = new ArrayList<Patient>(); List<Obs> observations = new ArrayList<Obs>(); List<Form> forms = new ArrayList<Form>(); for (Integer patientId : cohort.getMemberIds()) { Patient patient = Context.getPatientService().getPatient(patientId); observations.addAll(Context.getObsService().getObservationsByPerson(patient)); patients.add(patient); } Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class); serializer.write(outputStream, patients); serializer.write(outputStream, observations); serializer.write(outputStream, forms); outputStream.close(); GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))); DataInputStream dataInputStream = new DataInputStream(inputStream); // total number of patients Integer patientCounter = dataInputStream.readInt(); System.out.println("Patient Counter: " + patientCounter); for (int i = 0; i < patientCounter; i++) { System.out.println("=================Patient====================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Family Name: " + dataInputStream.readUTF()); System.out.println("Middle Name: " + dataInputStream.readUTF()); System.out.println("Last Name: " + dataInputStream.readUTF()); System.out.println("Gender: " + dataInputStream.readUTF()); System.out.println("Birth Date: " + dataInputStream.readUTF()); System.out.println("Identifier" + dataInputStream.readUTF()); } Integer obsCounter = dataInputStream.readInt(); for (int j = 0; j < obsCounter; j++) { System.out.println("==================Observation================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Concept Name: " + dataInputStream.readUTF()); byte type = dataInputStream.readByte(); if (type == ObsSerializer.TYPE_STRING) System.out.println("Value: " + dataInputStream.readUTF()); else if (type == ObsSerializer.TYPE_INT) System.out.println("Value: " + dataInputStream.readInt()); else if (type == ObsSerializer.TYPE_DOUBLE) System.out.println("Value: " + dataInputStream.readDouble()); else if (type == ObsSerializer.TYPE_DATE) System.out.println("Value: " + dataInputStream.readUTF()); System.out.println("Time: " + dataInputStream.readUTF()); } Integer formCounter = dataInputStream.readInt(); for (int j = 0; j < formCounter; j++) { System.out.println("==================Form================="); System.out.println("Form Id: " + dataInputStream.readInt()); } System.out.println(); inputStream.close(); }
From source file:com.project.qrypto.keymanagement.KeyManager.java
/** * Load a Keystore. Uses either internal or external memory depending on settings. * @param context the context to use./* ww w. j a v a 2s. c om*/ * * @throws IOException if the stream is bad * @throws InvalidCipherTextException if the key or data is bad */ public void load(Context context) throws IOException, InvalidCipherTextException { DataInputStream stream = null; if (passwordProtected) { byte[] data = AES.handle(false, IOUtils.toByteArray(getAssociatedInFileStream(context)), keyStoreKey); stream = new DataInputStream(new ByteArrayInputStream(data)); } else { stream = new DataInputStream(getAssociatedInFileStream(context)); } String type = stream.readUTF(); if (type.equals("RCYTHR1")) { //Valid int count = stream.readInt(); for (int i = 0; i < count; ++i) { String name = stream.readUTF(); lookup.put(name, Key.readData(stream)); } } else { throw new IOException("Bad Format"); } storeLoaded = true; }
From source file:org.apache.hadoop.hive.ql.exec.tez.DynamicPartitionPruner.java
@SuppressWarnings("deprecation") @VisibleForTesting//w w w .j a v a 2s . c o m protected String processPayload(ByteBuffer payload, String sourceName) throws SerDeException, IOException { DataInputStream in = new DataInputStream(new ByteBufferBackedInputStream(payload)); try { String columnName = in.readUTF(); LOG.info("Source of event: " + sourceName); List<SourceInfo> infos = this.sourceInfoMap.get(sourceName); if (infos == null) { throw new IllegalStateException("no source info for event source: " + sourceName); } SourceInfo info = null; for (SourceInfo si : infos) { if (columnName.equals(si.columnName)) { info = si; break; } } if (info == null) { throw new IllegalStateException("no source info for column: " + columnName); } if (info.skipPruning.get()) { // Marked as skipped previously. Don't bother processing the rest of the payload. } else { boolean skip = in.readBoolean(); if (skip) { info.skipPruning.set(true); } else { while (payload.hasRemaining()) { writable.readFields(in); Object row = info.deserializer.deserialize(writable); Object value = info.soi.getStructFieldData(row, info.field); value = ObjectInspectorUtils.copyToStandardObject(value, info.fieldInspector); if (LOG.isDebugEnabled()) { LOG.debug("Adding: " + value + " to list of required partitions"); } info.values.add(value); } } } } finally { if (in != null) { in.close(); } } return sourceName; }
From source file:SearchMixedRecordDataTypeExample.java
public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true);// w w w . j a v a 2s . 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[] = { "Adam", "Bob", "Mary" }; int outputInteger[] = { 15, 10, 5 }; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for (int x = 0; x < 3; x++) { outputDataStream.writeUTF(outputString[x]); outputDataStream.writeInt(outputInteger[x]); 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; byte[] byteInputData = new byte[300]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); if (recordstore.getNumRecords() > 0) { filter = new Filter("Mary"); recordEnumeration = recordstore.enumerateRecords(filter, null, false); while (recordEnumeration.hasNextElement()) { recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); inputString = inputDataStream.readUTF() + " " + inputDataStream.readInt(); alert = new Alert("Reading", inputString, null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } inputStream.close(); } 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"); filter.filterClose(); 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:SortMixedRecordDataTypeExample.java
public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true);// w w w . j a v a 2 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[] = { "Mary", "Bob", "Adam" }; int outputInteger[] = { 15, 10, 5 }; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for (int x = 0; x < 3; x++) { outputDataStream.writeUTF(outputString[x]); outputDataStream.writeInt(outputInteger[x]); 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 = new String[3]; int z = 0; byte[] byteInputData = new byte[300]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); StringBuffer buffer = new StringBuffer(); comparator = new Comparator(); recordEnumeration = recordstore.enumerateRecords(null, comparator, false); while (recordEnumeration.hasNextElement()) { recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); buffer.append(inputDataStream.readUTF()); buffer.append(inputDataStream.readInt()); buffer.append("\n"); inputDataStream.reset(); } alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); inputDataStream.close(); inputStream.close(); } 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"); comparator.compareClose(); recordEnumeration.destroy(); } catch (Exception error) { alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } } }