List of usage examples for java.io DataOutputStream writeUTF
public final void writeUTF(String str) throws IOException
From source file:org.sakaiproject.nakamura.auth.trusted.TokenStore.java
/** * Save all the secureKeys to file//from w w w . j av a2s . c o m */ @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", justification = "Could be injected from annother bundle") private void saveLocalSecretKeys() { FileOutputStream fout = null; DataOutputStream keyOutputStream = null; try { File parent = tokenFile.getAbsoluteFile().getParentFile(); LOG.debug("Saving Local Secret Keys to {} ", tokenFile.getAbsoluteFile()); if (!parent.exists()) { parent.mkdirs(); } fout = new FileOutputStream(tmpTokenFile); keyOutputStream = new DataOutputStream(fout); keyOutputStream.writeInt(secretKeyId); keyOutputStream.writeLong(nextUpdate); for (int i = 0; i < secretKeyRingBuffer.length; i++) { if (secretKeyRingBuffer[i] == null) { keyOutputStream.writeInt(0); } else { keyOutputStream.writeInt(1); keyOutputStream.writeLong(secretKeyRingBuffer[i].getExpires()); keyOutputStream.writeUTF(secretKeyRingBuffer[i].getServerId()); byte[] b = secretKeyRingBuffer[i].getSecretKey().getEncoded(); keyOutputStream.writeInt(b.length); keyOutputStream.write(b); } } keyOutputStream.close(); if (!tmpTokenFile.renameTo(tokenFile)) { LOG.error( "Failed to save cookie keys, rename of tokenFile failed. Reload of secure token keys will fail while this is happening. "); } } catch (IOException e) { LOG.error("Failed to save cookie keys " + e.getMessage()); } finally { try { keyOutputStream.close(); } catch (Exception e) { } try { fout.close(); } catch (Exception e) { } } }
From source file:org.spout.engine.filesystem.WorldFiles.java
public static void writeColumn(OutputStream out, SpoutColumn column, AtomicInteger lowestY, BlockMaterial[][] topmostBlocks) { DataOutputStream dataStream = new DataOutputStream(out); try {/* ww w . ja va2 s . c o m*/ for (int x = 0; x < SpoutColumn.BLOCKS.SIZE; x++) { for (int z = 0; z < SpoutColumn.BLOCKS.SIZE; z++) { dataStream.writeInt(column.getAtomicInteger(x, z).get()); } } dataStream.writeInt(COLUMN_VERSION); dataStream.writeInt(lowestY.get()); StringMap global = ((SpoutEngine) Spout.getEngine()).getEngineItemMap(); StringMap itemMap; itemMap = column.getWorld().getItemMap(); for (int x = 0; x < SpoutColumn.BLOCKS.SIZE; x++) { for (int z = 0; z < SpoutColumn.BLOCKS.SIZE; z++) { Material m = topmostBlocks[x][z]; if (m == null) { dataStream.writeBoolean(false); continue; } else { dataStream.writeBoolean(true); } short blockId = m.getId(); short blockData = m.getData(); blockId = (short) global.convertTo(itemMap, blockId); dataStream.writeInt(BlockFullState.getPacked(blockId, blockData)); } } //Biome manager is serialized with: // - boolean, if a biome manager exists // - String, the class name // - int, the number of bytes of data to read // - byte[], size of the above int in length BiomeManager manager = column.getBiomeManager(); if (manager != null) { dataStream.writeBoolean(true); dataStream.writeUTF(manager.getClass().getName()); byte[] data = manager.serialize(); dataStream.writeInt(data.length); dataStream.write(data); } else { dataStream.writeBoolean(false); } dataStream.flush(); } catch (IOException e) { Spout.getLogger() .severe("Error writing column height-map for column" + column.getX() + ", " + column.getZ()); } }
From source file:org.apache.hadoop.io.TestArrayOutputStream.java
private void runComparison(ArrayOutputStream aos, DataOutputStream dos, ByteArrayOutputStream bos) throws IOException { Random r = new Random(); // byte/*w ww .j av a 2s. c om*/ int b = r.nextInt(128); aos.write(b); dos.write(b); // byte[] byte[] bytes = new byte[10]; r.nextBytes(bytes); aos.write(bytes, 0, 10); dos.write(bytes, 0, 10); // Byte aos.writeByte(b); dos.writeByte(b); // boolean boolean bool = r.nextBoolean(); aos.writeBoolean(bool); dos.writeBoolean(bool); // short short s = (short) r.nextInt(); aos.writeShort(s); dos.writeShort(s); // char int c = r.nextInt(); aos.writeChar(c); dos.writeChar(c); // int int i = r.nextInt(); aos.writeInt(i); dos.writeInt(i); // long long l = r.nextLong(); aos.writeLong(l); dos.writeLong(l); // float float f = r.nextFloat(); aos.writeFloat(f); dos.writeFloat(f); // double double d = r.nextDouble(); aos.writeDouble(d); dos.writeDouble(d); // strings String str = RandomStringUtils.random(20); aos.writeBytes(str); aos.writeChars(str); aos.writeUTF(str); dos.writeBytes(str); dos.writeChars(str); dos.writeUTF(str); byte[] expected = bos.toByteArray(); assertEquals(expected.length, aos.size()); byte[] actual = new byte[aos.size()]; System.arraycopy(aos.getBytes(), 0, actual, 0, actual.length); // serialized bytes should be the same assertTrue(Arrays.equals(expected, actual)); }
From source file:org.grails.gsp.compiler.GroovyPageParser.java
public void writeHtmlParts(File filename) throws IOException { DataOutputStream dataOut = null; try {/* w ww. j a v a 2s . c om*/ dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); dataOut.writeInt(htmlParts.size()); for (String part : htmlParts) { dataOut.writeUTF(part); } } finally { SpringIOUtils.closeQuietly(dataOut); } }
From source file:org.codehaus.groovy.grails.web.pages.GroovyPageParser.java
public void writeHtmlParts(File filename) throws IOException { DataOutputStream dataOut = null; try {//ww w. j av a2 s . c o m dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); dataOut.writeInt(htmlParts.size()); for (String part : htmlParts) { dataOut.writeUTF(part); } } finally { IOUtils.closeQuietly(dataOut); } }
From source file:org.openxdata.server.servlet.DataImportServletTest.java
@Test @Ignore("this is an integration test for localhost") public void integrationTest() throws Exception { final String urlString = "http://localhost:8888/org.openxdata.server.admin.OpenXDataServerAdmin/dataimport?msisdn=2222222"; final String userName = "dagmar"; final String password = "dagmar"; // open url connection URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setDoOutput(true);/*from w ww. j av a2 s .c om*/ con.setDoInput(true); con.setChunkedStreamingMode(1024); // stuff the Authorization request header byte[] encodedPassword = (userName + ":" + password).getBytes(); con.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(encodedPassword))); // put the form data on the output stream DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeByte(new Integer(1)); String data = "<?xml version='1.0' encoding='UTF-8' ?><cmt_lesotho_tlpp_session_report_v1 formKey=\"cmt_lesotho_tlpp_session_report_v1\" id=\"2\" name=\"Treatment literacy programme_v1\" xmlns:xf=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <name>TEEHEE</name> <session_type>treatment_literacy_session</session_type> <session_date>2010-04-15</session_date> <district>berea</district> <session_location>secondary_school</session_location> <session_location_other/> <session_place_name>dsds</session_place_name> <participant_number_male>1</participant_number_male> <participant_number_female>2</participant_number_female> <session_start_time>10:22:00</session_start_time> <session_finish_time>01:22:00</session_finish_time> <session_topic>children_HIV_incl_ARVs</session_topic> <session_topic_other/> <support_group>false</support_group> <one_on_one_session>false</one_on_one_session> <CMT_AV_kit>false</CMT_AV_kit> <CMT_DVD>false</CMT_DVD> <CMT_co_facilitator>false</CMT_co_facilitator> <co_facilitator_name/> <clinic_hospital_department/> <clinic_hospital_department_other/> <clinic_hospital_TV/> <school_grade>12</school_grade> <school_CMT_materials>true</school_CMT_materials> <session_confirmed>true</session_confirmed></cmt_lesotho_tlpp_session_report_v1>"; out.writeUTF(data); out.flush(); // pull the information back from the URL InputStream is = con.getInputStream(); StringBuffer buf = new StringBuffer(); int c; while ((c = is.read()) != -1) { buf.append((char) c); } con.disconnect(); // output the information System.out.println(buf); }
From source file:org.prorefactor.refactor.PUB.java
private void writeSchemaSegment(DataOutputStream out, List rootSymbols) throws IOException { for (Iterator it = rootSymbols.iterator(); it.hasNext();) { Object obj = it.next();//from w w w .j av a2s . c o m if (obj instanceof TableBuffer) { Table table = ((TableBuffer) obj).getTable(); if (table.getStoretype() != IConstants.ST_DBTABLE) continue; writeSchema_addTable(table); continue; } if (obj instanceof FieldBuffer) { Field field = ((FieldBuffer) obj).getField(); Table table = field.getTable(); if (table.getStoretype() != IConstants.ST_DBTABLE) continue; TableRef tableRef = writeSchema_addTable(table); tableRef.fieldMap.put(field.getName().toLowerCase(), field.getName()); } } for (TableRef tableRef : tableMap.values()) { out.writeUTF(tableRef.name); for (String fieldName : tableRef.fieldMap.values()) { out.writeUTF(fieldName); } out.writeUTF(""); // terminate the list of fields in the table } out.writeUTF(""); // terminate the schema segment }
From source file:runtime.starter.MPJYarnClient.java
public void run() throws Exception { Map<String, String> map = System.getenv(); try {/*from w w w . jav a 2 s.c om*/ mpjHomeDir = map.get("MPJ_HOME"); if (mpjHomeDir == null) { throw new Exception("[MPJRun.java]:MPJ_HOME environment found.."); } } catch (Exception exc) { System.out.println("[MPJRun.java]:" + exc.getMessage()); exc.printStackTrace(); return; } // Copy the application master jar to HDFS // Create a local resource to point to the destination jar path FileSystem fs = FileSystem.get(conf); /* Path dataset = new Path(fs.getHomeDirectory(),"/dataset"); FileStatus datasetFile = fs.getFileStatus(dataset); BlockLocation myBlocks [] = fs.getFileBlockLocations(datasetFile,0,datasetFile.getLen()); for(BlockLocation b : myBlocks){ System.out.println("\n--------------------"); System.out.println("Length "+b.getLength()); for(String host : b.getHosts()){ System.out.println("host "+host); } } */ Path source = new Path(mpjHomeDir + "/lib/mpj-app-master.jar"); String pathSuffix = hdfsFolder + "mpj-app-master.jar"; Path dest = new Path(fs.getHomeDirectory(), pathSuffix); if (debugYarn) { logger.info("Uploading mpj-app-master.jar to: " + dest.toString()); } fs.copyFromLocalFile(false, true, source, dest); FileStatus destStatus = fs.getFileStatus(dest); Path wrapperSource = new Path(mpjHomeDir + "/lib/mpj-yarn-wrapper.jar"); String wrapperSuffix = hdfsFolder + "mpj-yarn-wrapper.jar"; Path wrapperDest = new Path(fs.getHomeDirectory(), wrapperSuffix); if (debugYarn) { logger.info("Uploading mpj-yarn-wrapper.jar to: " + wrapperDest.toString()); } fs.copyFromLocalFile(false, true, wrapperSource, wrapperDest); Path userJar = new Path(jarPath); String userJarSuffix = hdfsFolder + "user-code.jar"; Path userJarDest = new Path(fs.getHomeDirectory(), userJarSuffix); if (debugYarn) { logger.info("Uploading user-code.jar to: " + userJarDest.toString()); } fs.copyFromLocalFile(false, true, userJar, userJarDest); YarnConfiguration conf = new YarnConfiguration(); YarnClient yarnClient = YarnClient.createYarnClient(); yarnClient.init(conf); yarnClient.start(); if (debugYarn) { YarnClusterMetrics metrics = yarnClient.getYarnClusterMetrics(); logger.info("\nNodes Information"); logger.info("Number of NM: " + metrics.getNumNodeManagers() + "\n"); List<NodeReport> nodeReports = yarnClient.getNodeReports(NodeState.RUNNING); for (NodeReport n : nodeReports) { logger.info("NodeId: " + n.getNodeId()); logger.info("RackName: " + n.getRackName()); logger.info("Total Memory: " + n.getCapability().getMemory()); logger.info("Used Memory: " + n.getUsed().getMemory()); logger.info("Total vCores: " + n.getCapability().getVirtualCores()); logger.info("Used vCores: " + n.getUsed().getVirtualCores() + "\n"); } } logger.info("Creating server socket at HOST " + serverName + " PORT " + serverPort + " \nWaiting for " + np + " processes to connect..."); // Creating a server socket for incoming connections try { servSock = new ServerSocket(serverPort); infoSock = new ServerSocket(); TEMP_PORT = findPort(infoSock); } catch (Exception e) { e.printStackTrace(); } // Create application via yarnClient YarnClientApplication app = yarnClient.createApplication(); GetNewApplicationResponse appResponse = app.getNewApplicationResponse(); int maxMem = appResponse.getMaximumResourceCapability().getMemory(); if (debugYarn) { logger.info("Max memory capability resources in cluster: " + maxMem); } if (amMem > maxMem) { amMem = maxMem; logger.info("AM memory specified above threshold of cluster " + "Using maximum memory for AM container: " + amMem); } int maxVcores = appResponse.getMaximumResourceCapability().getVirtualCores(); if (debugYarn) { logger.info("Max vCores capability resources in cluster: " + maxVcores); } if (amCores > maxVcores) { amCores = maxVcores; logger.info("AM virtual cores specified above threshold of cluster " + "Using maximum virtual cores for AM container: " + amCores); } // Set up the container launch context for the application master ContainerLaunchContext amContainer = Records.newRecord(ContainerLaunchContext.class); List<String> commands = new ArrayList<String>(); commands.add("$JAVA_HOME/bin/java"); commands.add("-Xmx" + amMem + "m"); commands.add("runtime.starter.MPJAppMaster"); commands.add("--np"); commands.add(String.valueOf(np)); commands.add("--serverName"); commands.add(serverName); //server name commands.add("--ioServerPort"); commands.add(Integer.toString(serverPort)); //server port commands.add("--deviceName"); commands.add(deviceName); //device name commands.add("--className"); commands.add(className); //class name commands.add("--wdir"); commands.add(workingDirectory); //wdir commands.add("--psl"); commands.add(Integer.toString(psl)); //protocol switch limit commands.add("--wireUpPort"); commands.add(String.valueOf(TEMP_PORT)); //for sharing ports & rank commands.add("--wrapperPath"); commands.add(wrapperDest.toString());//MPJYarnWrapper.jar HDFS path commands.add("--userJarPath"); commands.add(userJarDest.toString());//User Jar File HDFS path commands.add("--mpjContainerPriority"); commands.add(mpjContainerPriority);// priority for mpj containers commands.add("--containerMem"); commands.add(containerMem); commands.add("--containerCores"); commands.add(containerCores); if (debugYarn) { commands.add("--debugYarn"); } if (appArgs != null) { commands.add("--appArgs"); for (int i = 0; i < appArgs.length; i++) { commands.add(appArgs[i]); } } amContainer.setCommands(commands); //set commands // Setup local Resource for ApplicationMaster LocalResource appMasterJar = Records.newRecord(LocalResource.class); appMasterJar.setResource(ConverterUtils.getYarnUrlFromPath(dest)); appMasterJar.setSize(destStatus.getLen()); appMasterJar.setTimestamp(destStatus.getModificationTime()); appMasterJar.setType(LocalResourceType.ARCHIVE); appMasterJar.setVisibility(LocalResourceVisibility.APPLICATION); amContainer.setLocalResources(Collections.singletonMap("mpj-app-master.jar", appMasterJar)); // Setup CLASSPATH for ApplicationMaster // Setting up the environment Map<String, String> appMasterEnv = new HashMap<String, String>(); setupAppMasterEnv(appMasterEnv); amContainer.setEnvironment(appMasterEnv); // Set up resource type requirements for ApplicationMaster Resource capability = Records.newRecord(Resource.class); capability.setMemory(amMem); capability.setVirtualCores(amCores); // Finally, set-up ApplicationSubmissionContext for the application ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext(); appContext.setApplicationName(appName); appContext.setAMContainerSpec(amContainer); appContext.setResource(capability); appContext.setQueue(yarnQueue); // queue Priority priority = Priority.newInstance(amPriority); appContext.setPriority(priority); ApplicationId appId = appContext.getApplicationId(); //Adding ShutDown Hook Runtime.getRuntime().addShutdownHook(new KillYarnApp(appId, yarnClient)); // Submit application System.out.println("Submitting Application: " + appContext.getApplicationName() + "\n"); try { isRunning = true; yarnClient.submitApplication(appContext); } catch (Exception exp) { System.err.println("Error Submitting Application"); exp.printStackTrace(); } // np = number of processes , + 1 for Application Master container IOMessagesThread[] ioThreads = new IOMessagesThread[np + 1]; peers = new String[np]; socketList = new Vector<Socket>(); int wport = 0; int rport = 0; int rank = 0; // np + 1 IOThreads for (int i = 0; i < (np + 1); i++) { try { sock = servSock.accept(); //start IO thread to read STDOUT and STDERR from wrappers IOMessagesThread io = new IOMessagesThread(sock); ioThreads[i] = io; ioThreads[i].start(); } catch (Exception e) { System.err.println("Error accepting connection from peer socket.."); e.printStackTrace(); } } // Loop to read port numbers from Wrapper.java processes // and to create WRAPPER_INFO (containing all IPs and ports) String WRAPPER_INFO = "#Peer Information"; for (int i = np; i > 0; i--) { try { sock = infoSock.accept(); DataOutputStream out = new DataOutputStream(sock.getOutputStream()); DataInputStream in = new DataInputStream(sock.getInputStream()); if (in.readUTF().startsWith("Sending Info")) { wport = in.readInt(); rport = in.readInt(); rank = in.readInt(); peers[rank] = ";" + sock.getInetAddress().getHostAddress() + "@" + rport + "@" + wport + "@" + rank; socketList.add(sock); } } catch (Exception e) { System.err.println("[MPJYarnClient.java]: Error accepting" + " connection from peer socket!"); e.printStackTrace(); } } for (int i = 0; i < np; i++) { WRAPPER_INFO += peers[i]; } // Loop to broadcast WRAPPER_INFO to all Wrappers for (int i = np; i > 0; i--) { try { sock = socketList.get(np - i); DataOutputStream out = new DataOutputStream(sock.getOutputStream()); out.writeUTF(WRAPPER_INFO); out.flush(); sock.close(); } catch (Exception e) { System.err.println("[MPJYarnClient.java]: Error closing" + " connection from peer socket.."); e.printStackTrace(); } } try { infoSock.close(); } catch (IOException exp) { exp.printStackTrace(); } // wait for all IO Threads to complete for (int i = 0; i < (np + 1); i++) { ioThreads[i].join(); } isRunning = true; System.out.println("\nApplication Statistics!"); while (true) { appReport = yarnClient.getApplicationReport(appId); appState = appReport.getYarnApplicationState(); fStatus = appReport.getFinalApplicationStatus(); if (appState == YarnApplicationState.FINISHED) { isRunning = false; if (fStatus == FinalApplicationStatus.SUCCEEDED) { System.out.println("State: " + fStatus); } else { System.out.println("State: " + fStatus); } break; } else if (appState == YarnApplicationState.KILLED) { isRunning = false; System.out.println("State: " + appState); break; } else if (appState == YarnApplicationState.FAILED) { isRunning = false; System.out.println("State: " + appState); break; } Thread.sleep(100); } try { if (debugYarn) { logger.info("Cleaning the files from hdfs: "); logger.info("1) " + dest.toString()); logger.info("2) " + wrapperDest.toString()); logger.info("3) " + userJarDest.toString()); } fs.delete(dest); fs.delete(wrapperDest); fs.delete(userJarDest); } catch (IOException exp) { exp.printStackTrace(); } System.out.println("Application ID: " + appId + "\n" + "Application User: " + appReport.getUser() + "\n" + "RM Queue: " + appReport.getQueue() + "\n" + "Start Time: " + appReport.getStartTime() + "\n" + "Finish Time: " + appReport.getFinishTime()); }
From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java
private byte[] convertStringMapToBytes(final Map<String, String> arguments) throws AMQStoreException { byte[] argumentBytes; if (arguments == null) { argumentBytes = new byte[0]; } else {//from w w w .j a v a 2s . com ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); try { dos.writeInt(arguments.size()); for (Map.Entry<String, String> arg : arguments.entrySet()) { dos.writeUTF(arg.getKey()); dos.writeUTF(arg.getValue()); } } catch (IOException e) { // This should never happen throw new AMQStoreException(e.getMessage(), e); } argumentBytes = bos.toByteArray(); } return argumentBytes; }
From source file:com.android.leanlauncher.LauncherTransitionable.java
private static void writeConfiguration(Context context, LocaleConfiguration configuration) { DataOutputStream out = null; try {/* ww w. j av a 2 s. c om*/ out = new DataOutputStream(context.openFileOutput(LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE)); out.writeUTF(configuration.locale); out.writeInt(configuration.mcc); out.writeInt(configuration.mnc); out.flush(); } catch (FileNotFoundException e) { // Ignore } catch (IOException e) { //noinspection ResultOfMethodCallIgnored context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // Ignore } } } }