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

The following examples show how to use java.io.DataOutputStream#writeBytes() . 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: Manifest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes the Manifest to the specified OutputStream.
 * Attributes.Name.MANIFEST_VERSION must be set in
 * MainAttributes prior to invoking this method.
 *
 * @param out the output stream
 * @exception IOException if an I/O error has occurred
 * @see #getMainAttributes
 */
public void write(OutputStream out) throws IOException {
    DataOutputStream dos = new DataOutputStream(out);
    // Write out the main attributes for the manifest
    attr.writeMain(dos);
    // Now write out the pre-entry attributes
    Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Attributes> e = it.next();
        StringBuffer buffer = new StringBuffer("Name: ");
        String value = e.getKey();
        if (value != null) {
            byte[] vb = value.getBytes("UTF8");
            value = new String(vb, 0, 0, vb.length);
        }
        buffer.append(value);
        buffer.append("\r\n");
        make72Safe(buffer);
        dos.writeBytes(buffer.toString());
        e.getValue().write(dos);
    }
    dos.flush();
}
 
Example 2
Source File: Manifest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes the Manifest to the specified OutputStream.
 * Attributes.Name.MANIFEST_VERSION must be set in
 * MainAttributes prior to invoking this method.
 *
 * @param out the output stream
 * @exception IOException if an I/O error has occurred
 * @see #getMainAttributes
 */
public void write(OutputStream out) throws IOException {
    DataOutputStream dos = new DataOutputStream(out);
    // Write out the main attributes for the manifest
    attr.writeMain(dos);
    // Now write out the pre-entry attributes
    Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Attributes> e = it.next();
        StringBuffer buffer = new StringBuffer("Name: ");
        String value = e.getKey();
        if (value != null) {
            byte[] vb = value.getBytes("UTF8");
            value = new String(vb, 0, 0, vb.length);
        }
        buffer.append(value);
        buffer.append("\r\n");
        make72Safe(buffer);
        dos.writeBytes(buffer.toString());
        e.getValue().write(dos);
    }
    dos.flush();
}
 
Example 3
Source File: Attributes.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
void write(DataOutputStream os) throws IOException {
    Iterator<Map.Entry<Object, Object>> it = entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Object, Object> e = it.next();
        StringBuffer buffer = new StringBuffer(
                                    ((Name)e.getKey()).toString());
        buffer.append(": ");

        String value = (String)e.getValue();
        if (value != null) {
            byte[] vb = value.getBytes("UTF8");
            value = new String(vb, 0, 0, vb.length);
        }
        buffer.append(value);

        buffer.append("\r\n");
        Manifest.make72Safe(buffer);
        os.writeBytes(buffer.toString());
    }
    os.writeBytes("\r\n");
}
 
Example 4
Source File: SocketTestUtils.java    From reactive-ipc-jvm with Apache License 2.0 6 votes vote down vote up
public static String read(String host, int port, String dataToSend) {
    try {
        Socket socket = new Socket(host, port);
        InputStreamReader reader = new InputStreamReader(socket.getInputStream());
        if (dataToSend != null) {
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeBytes(dataToSend);
        }
        StringBuilder content = new StringBuilder();
        int c = reader.read();
        while (c != -1) {
            content.append((char)c);
            c = reader.read();
        }
        reader.close();
        return content.toString();
    }
    catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 5
Source File: Manifest.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the Manifest to the specified OutputStream.
 * Attributes.Name.MANIFEST_VERSION must be set in
 * MainAttributes prior to invoking this method.
 *
 * @param out the output stream
 * @exception IOException if an I/O error has occurred
 * @see #getMainAttributes
 */
public void write(OutputStream out) throws IOException {
    DataOutputStream dos = new DataOutputStream(out);
    // Write out the main attributes for the manifest
    attr.writeMain(dos);
    // Now write out the pre-entry attributes
    Iterator it = entries.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry e = (Map.Entry)it.next();
        StringBuffer buffer = new StringBuffer("Name: ");
        String value = (String)e.getKey();
        if (value != null) {
            byte[] vb = value.getBytes("UTF8");
            value = new String(vb, 0, 0, vb.length);
        }
        buffer.append(value);
        buffer.append("\r\n");
        make72Safe(buffer);
        dos.writeBytes(buffer.toString());
        ((Attributes)e.getValue()).write(dos);
    }
    dos.flush();
}
 
Example 6
Source File: MailSenderUtil.java    From kardio with Apache License 2.0 6 votes vote down vote up
/**
 * Send message to Webhook URL
 * 
 * @param subscription
 * @throws IOException
 */
public void sentMessageToWebhook(String webhookUrl, String message) throws IOException {
    
    Log.debug("Webhook Message : " + message);
    URL myurl = new URL(webhookUrl);
    HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
    con.setRequestMethod("POST");

    con.setRequestProperty("Content-length", String.valueOf(message.length()));
    con.setRequestProperty("Content-Type", "application/json");
    con.setDoOutput(true);
    con.setDoInput(true);

    DataOutputStream output = new DataOutputStream(con.getOutputStream());
    output.writeBytes(message);
    output.flush();
    output.close();

    BufferedInputStream inStream = new BufferedInputStream(con.getInputStream());
    byte[] b = new byte[256];
    inStream.read(b);
    Log.debug("Response : " + new String(b));
}
 
Example 7
Source File: TestMapProgress.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void createInputFile(Path rootDir) throws IOException {
  if(fs.exists(rootDir)){
    fs.delete(rootDir, true);
  }
  
  String str = "The quick brown fox\n" + "The brown quick fox\n"
  + "The fox brown quick\n";
  DataOutputStream inpFile = fs.create(new Path(rootDir, "part-0"));
  inpFile.writeBytes(str);
  inpFile.close();
}
 
Example 8
Source File: TestJobInProgress.java    From RDFS with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
JobConf configure(Class MapClass,Class ReduceClass, int maps, int reducers,
                  boolean locality) 
throws Exception {
  JobConf jobConf = mrCluster.createJobConf();
  final Path inDir = new Path("./failjob/input");
  final Path outDir = new Path("./failjob/output");
  String input = "Test failing job.\n One more line";
  FileSystem inFs = inDir.getFileSystem(jobConf);
  FileSystem outFs = outDir.getFileSystem(jobConf);
  outFs.delete(outDir, true);
  if (!inFs.mkdirs(inDir)) {
    throw new IOException("create directory failed" + inDir.toString());
  }

  DataOutputStream file = inFs.create(new Path(inDir, "part-0"));
  file.writeBytes(input);
  file.close();
  jobConf.setJobName("failmaptask");
  if (locality) {
    jobConf.setInputFormat(TextInputFormat.class);
  } else {
    jobConf.setInputFormat(UtilsForTests.RandomInputFormat.class);
  }
  jobConf.setOutputKeyClass(Text.class);
  jobConf.setOutputValueClass(Text.class);
  jobConf.setMapperClass(MapClass);
  jobConf.setCombinerClass(ReduceClass);
  jobConf.setReducerClass(ReduceClass);
  FileInputFormat.setInputPaths(jobConf, inDir);
  FileOutputFormat.setOutputPath(jobConf, outDir);
  jobConf.setNumMapTasks(maps);
  jobConf.setNumReduceTasks(reducers);
  return jobConf; 
}
 
Example 9
Source File: RequestExecutionHelper.java    From cloud-espm-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to execute a HTTP PUT request
 * 
 * @param serviceEndPoint
 *            OData service url
 * @param payLoad
 *            xml payload for request body
 * @param secure
 *            True if credentials need to be sent with request
 * @return http reponse of request
 * @throws IOException
 */
public static HttpResponse executePutRequest(String serviceEndPoint,
		String payLoad, boolean secure) throws IOException {
	URL url = (secure) ? new URL(SERVICE_ROOT_URI + "secure/"
			+ serviceEndPoint)
			: new URL(SERVICE_ROOT_URI + serviceEndPoint);
	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	try {
		connection.setRequestMethod("PUT");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		if (secure) {
			connection.setRequestProperty("Authorization", AUTHORIZATION);
		}
		connection.setRequestProperty("Content-Type", "application/xml");
		connection.setRequestProperty("Accept", "application/xml");
		DataOutputStream outStream = new DataOutputStream(
				connection.getOutputStream());
		try {
			outStream.writeBytes(payLoad);
			outStream.close();
			return new HttpResponse(connection);
		} finally {
			outStream.close();
		}
	} finally {
		connection.disconnect();
	}

}
 
Example 10
Source File: IdentityTest.java    From hiped2 with Apache License 2.0 5 votes vote down vote up
@Test
public void run() throws Exception {
  Path inputPath = new Path("/tmp/mrtest/input");
  Path outputPath = new Path("/tmp/mrtest/output");

  Configuration conf = new Configuration();

  conf.set("mapred.job.tracker", "local");
  conf.set("fs.default.name", "file:///");

  FileSystem fs = FileSystem.get(conf);
  if (fs.exists(outputPath)) {
    fs.delete(outputPath, true);
  }
  if (fs.exists(inputPath)) {
    fs.delete(inputPath, true);
  }
  fs.mkdirs(inputPath);

  String input = "foo\tbar";
  DataOutputStream file = fs.create(new Path(inputPath, "part-" + 0));
  file.writeBytes(input);
  file.close();

  Job job = runJob(conf, inputPath, outputPath);
  assertTrue(job.isSuccessful());

  List<String> lines =
      IOUtils.readLines(fs.open(new Path(outputPath, "part-r-00000")));

  assertEquals(1, lines.size());
  String[] parts = StringUtils.split(lines.get(0), "\t");
  assertEquals("foo", parts[0]);
  assertEquals("bar", parts[1]);
}
 
Example 11
Source File: Distcp.java    From aegisthus with Apache License 2.0 5 votes vote down vote up
protected void writeManifest(Job job, List<FileStatus> files) throws IOException {
	Path out = new Path(job.getConfiguration().get(OPT_DISTCP_TARGET));
	FileSystem fsOut = out.getFileSystem(job.getConfiguration());
	DataOutputStream dos = fsOut.create(new Path(out, "_manifest/.manifest"));
	for (FileStatus file : files) {
		Path output = new Path(out, file.getPath().getName());
		dos.writeBytes(output.toUri().toString());
		dos.write('\n');
	}
	dos.close();
}
 
Example 12
Source File: TestMiniMRWithDFS.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public static TestResult launchWordCount(JobConf conf,
                                         Path inDir,
                                         Path outDir,
                                         String input,
                                         int numMaps,
                                         int numReduces) throws IOException {
  FileSystem inFs = inDir.getFileSystem(conf);
  FileSystem outFs = outDir.getFileSystem(conf);
  outFs.delete(outDir, true);
  if (!inFs.mkdirs(inDir)) {
    throw new IOException("Mkdirs failed to create " + inDir.toString());
  }
  {
    DataOutputStream file = inFs.create(new Path(inDir, "part-0"));
    file.writeBytes(input);
    file.close();
  }
  conf.setJobName("wordcount");
  conf.setInputFormat(TextInputFormat.class);
  
  // the keys are words (strings)
  conf.setOutputKeyClass(Text.class);
  // the values are counts (ints)
  conf.setOutputValueClass(IntWritable.class);
  
  conf.setMapperClass(WordCount.MapClass.class);        
  conf.setCombinerClass(WordCount.Reduce.class);
  conf.setReducerClass(WordCount.Reduce.class);
  FileInputFormat.setInputPaths(conf, inDir);
  FileOutputFormat.setOutputPath(conf, outDir);
  conf.setNumMapTasks(numMaps);
  conf.setNumReduceTasks(numReduces);
  RunningJob job = JobClient.runJob(conf);
  return new TestResult(job, readOutput(outDir, conf));
}
 
Example 13
Source File: FileServiceApp.java    From product-ei with Apache License 2.0 5 votes vote down vote up
private void doPost(String url, String content) throws IOException {
	URL urlObj = new URL(url);
	URLConnection conn = urlObj.openConnection();
	conn.setDoInput(true);
	conn.setDoOutput(true);
	conn.setUseCaches(false);
	conn.setRequestProperty("Content-Type",	"application/x-www-form-urlencoded");
	DataOutputStream out = new DataOutputStream(conn.getOutputStream());
	out.writeBytes(content);
	out.flush();
	out.close();
	conn.getInputStream().close();
}
 
Example 14
Source File: SystemUtil.java    From AiXiaoChu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 启动进程处理事件
 * @param events
 * @return
 */
public static boolean onProcessEvents(String[] events){
	try {
		Process suProcess = Runtime.getRuntime().exec("su");  
		DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());  
		for (String event : events) {
			os.writeBytes(event + "\n");
			os.flush();
		}
		return true;
	} catch (IOException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 15
Source File: Attributes.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void writeMain(DataOutputStream out) throws IOException
{
    // write out the *-Version header first, if it exists
    String vername = Name.MANIFEST_VERSION.toString();
    String version = getValue(vername);
    if (version == null) {
        vername = Name.SIGNATURE_VERSION.toString();
        version = getValue(vername);
    }

    if (version != null) {
        out.writeBytes(vername+": "+version+"\r\n");
    }

    // write out all attributes except for the version
    // we wrote out earlier
    Iterator<Map.Entry<Object, Object>> it = entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Object, Object> e = it.next();
        String name = ((Name)e.getKey()).toString();
        if ((version != null) && ! (name.equalsIgnoreCase(vername))) {

            StringBuffer buffer = new StringBuffer(name);
            buffer.append(": ");

            String value = (String)e.getValue();
            if (value != null) {
                byte[] vb = value.getBytes("UTF8");
                value = new String(vb, 0, 0, vb.length);
            }
            buffer.append(value);

            buffer.append("\r\n");
            Manifest.make72Safe(buffer);
            out.writeBytes(buffer.toString());
        }
    }
    out.writeBytes("\r\n");
}
 
Example 16
Source File: TasksUtils.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
private static void setMobileDataEnabled(Context context, boolean enable) {
    //https://stackoverflow.com/questions/21511216/toggle-mobile-data-programmatically-on-android-4-4-2
    try {//4.4及以下
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Class aClass = mConnectivityManager.getClass();
        Class[] argsClass = new Class[1];
        argsClass[0] = boolean.class;
        Method method = aClass.getMethod("setMobileDataEnabled", argsClass);
        method.invoke(mConnectivityManager, enable);
    } catch (Exception e) {
        e.printStackTrace();
        try {//pri-app方法
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod("setDataEnabled", Boolean.TYPE);
            methodSet.invoke(tm, true);
        } catch (Exception ee) {
            ee.printStackTrace();
            try {//Root方法
                Process process = Runtime.getRuntime().exec("su");
                DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
                outputStream.writeBytes("svc data " + (enable ? "enable" : "disable") + "\n");
                outputStream.writeBytes("exit\n");
                outputStream.flush();
                process.waitFor();
                destroyProcess(outputStream, process);
            } catch (Exception eee) {//暂时无计可施……
                eee.printStackTrace();
                showToast(context, R.string.failed);
            }
        }
    }
}
 
Example 17
Source File: FileServiceApp.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void doPost(String url, String content) throws IOException {
	URL urlObj = new URL(url);
	URLConnection conn = urlObj.openConnection();
	conn.setDoInput(true);
	conn.setDoOutput(true);
	conn.setUseCaches(false);
	conn.setRequestProperty("Content-Type",	"application/x-www-form-urlencoded");
	DataOutputStream out = new DataOutputStream(conn.getOutputStream());
	out.writeBytes(content);
	out.flush();
	out.close();
	conn.getInputStream().close();
}
 
Example 18
Source File: SimpleMessage.java    From Kademlia with MIT License 5 votes vote down vote up
@Override
public void toStream(DataOutputStream out)
{
    try
    {
        out.writeInt(this.content.length());
        out.writeBytes(this.content);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
 
Example 19
Source File: OldAndroidDataInputStreamTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testDataInputStream() throws Exception {
    String str = "AbCdEfGhIjKlM\nOpQ\rStUvWxYz";
    ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream da = new ByteArrayInputStream(str.getBytes());

    DataInputStream a = new DataInputStream(aa);
    try {
        Assert.assertEquals(str, read(a));
    } finally {
        a.close();
    }

    DataInputStream b = new DataInputStream(ba);
    try {
        Assert.assertEquals("AbCdEfGhIj", read(b, 10));
    } finally {
        b.close();
    }

    DataInputStream c = new DataInputStream(ca);
    try {
        Assert.assertEquals("bdfhjl\np\rtvxz", skipRead(c));
    } finally {
        c.close();
    }

    DataInputStream d = new DataInputStream(da);
    try {
        assertEquals("AbCdEfGhIjKlM", d.readLine());
        assertEquals("OpQ", d.readLine());
        assertEquals("StUvWxYz", d.readLine());
    } finally {
        d.close();
    }

    ByteArrayOutputStream e = new ByteArrayOutputStream();
    DataOutputStream f = new DataOutputStream(e);
    try {
        f.writeBoolean(true);
        f.writeByte('a');
        f.writeBytes("BCD");
        f.writeChar('e');
        f.writeChars("FGH");
        f.writeUTF("ijklm");
        f.writeDouble(1);
        f.writeFloat(2);
        f.writeInt(3);
        f.writeLong(4);
        f.writeShort(5);
    } finally {
        f.close();
    }

    ByteArrayInputStream ga = new ByteArrayInputStream(e.toByteArray());
    DataInputStream g = new DataInputStream(ga);

    try {
        assertTrue(g.readBoolean());
        assertEquals('a', g.readByte());
        assertEquals(2, g.skipBytes(2));
        assertEquals('D', g.readByte());
        assertEquals('e', g.readChar());
        assertEquals('F', g.readChar());
        assertEquals('G', g.readChar());
        assertEquals('H', g.readChar());
        assertEquals("ijklm", g.readUTF());
        assertEquals(1, g.readDouble(), 0);
        assertEquals(2f, g.readFloat(), 0f);
        assertEquals(3, g.readInt());
        assertEquals(4, g.readLong());
        assertEquals(5, g.readShort());
    } finally {
        g.close();
    }
}
 
Example 20
Source File: TestSpecialCharactersInOutputPath.java    From RDFS with Apache License 2.0 4 votes vote down vote up
public static boolean launchJob(String fileSys,
                                     String jobTracker,
                                     JobConf conf,
                                     int numMaps,
                                     int numReduces) throws IOException {
  
  final Path inDir = new Path("/testing/input");
  final Path outDir = new Path("/testing/output");
  FileSystem fs = FileSystem.getNamed(fileSys, conf);
  fs.delete(outDir, true);
  if (!fs.mkdirs(inDir)) {
    LOG.warn("Can't create " + inDir);
    return false;
  }
  // generate an input file
  DataOutputStream file = fs.create(new Path(inDir, "part-0"));
  file.writeBytes("foo foo2 foo3");
  file.close();

  // use WordCount example
  FileSystem.setDefaultUri(conf, fileSys);
  conf.set("mapred.job.tracker", jobTracker);
  conf.setJobName("foo");

  conf.setInputFormat(TextInputFormat.class);
  conf.setOutputFormat(SpecialTextOutputFormat.class);
  conf.setOutputKeyClass(LongWritable.class);
  conf.setOutputValueClass(Text.class);
  conf.setMapperClass(IdentityMapper.class);        
  conf.setReducerClass(IdentityReducer.class);
  FileInputFormat.setInputPaths(conf, inDir);
  FileOutputFormat.setOutputPath(conf, outDir);
  conf.setNumMapTasks(numMaps);
  conf.setNumReduceTasks(numReduces);
    
  // run job and wait for completion
  RunningJob runningJob = JobClient.runJob(conf);
    
  try {
    assertTrue(runningJob.isComplete());
    assertTrue(runningJob.isSuccessful());
    assertTrue("Output folder not found!", fs.exists(new Path("/testing/output/" + OUTPUT_FILENAME)));
  } catch (NullPointerException npe) {
    // This NPE should no more happens
    fail("A NPE should not have happened.");
  }
        
  // return job result
  LOG.info("job is complete: " + runningJob.isSuccessful());
  return (runningJob.isSuccessful());
}