List of usage examples for java.io DataOutputStream write
public synchronized void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to the underlying output stream. From source file:com.cloudera.seismic.segy.SegyUnloader.java
private void write(Path path, DataOutputStream out, Configuration conf) throws Exception { System.out.println("Reading: " + path); SequenceFile.Reader reader = new SequenceFile.Reader(FileSystem.get(conf), path, conf); BytesWritable value = new BytesWritable(); while (reader.next(NullWritable.get(), value)) { out.write(value.getBytes(), 0, value.getLength()); }//from ww w. ja v a 2 s .c o m reader.close(); }
From source file:org.apache.hadoop.tools.mapred.filechunk.TestCopyChunkMapper.java
private static void touchFile(String path, long totalFileSize, boolean preserveBlockSize, ChecksumOpt checksumOpt) throws Exception { FileSystem fs;//from w ww .j a v a2 s . c o m DataOutputStream outputStream = null; try { fs = cluster.getFileSystem(); final Path qualifiedPath = new Path(path).makeQualified(fs.getUri(), fs.getWorkingDirectory()); final long blockSize = preserveBlockSize ? NON_DEFAULT_BLOCK_SIZE : fs.getDefaultBlockSize(qualifiedPath) * 2; FsPermission permission = FsPermission.getFileDefault().applyUMask(FsPermission.getUMask(fs.getConf())); outputStream = fs.create(qualifiedPath, permission, EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), 0, (short) (fs.getDefaultReplication(qualifiedPath) * 2), blockSize, null, checksumOpt); byte[] bytes = new byte[DEFAULT_FILE_SIZE]; long curFileSize = 0; int bufferLen = DEFAULT_FILE_SIZE; while (curFileSize < totalFileSize) { if (totalFileSize - curFileSize < DEFAULT_FILE_SIZE) bufferLen = (int) (totalFileSize - curFileSize); outputStream.write(bytes, 0, bufferLen); outputStream.flush(); curFileSize += bufferLen; } pathList.add(qualifiedPath); ++nFiles; FileStatus fileStatus = fs.getFileStatus(qualifiedPath); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getReplication()); } finally { IOUtils.cleanup(null, outputStream); } }
From source file:org.apache.hadoop.io.crypto.tool.CryptoApiTool.java
private void writeStream(CompressionInputStream input, DataOutputStream output) throws IOException { int read = 0; byte[] buffer = new byte[64 * 1024]; // while (0 < (read = input.read(buffer, 0, 64 * 1024))) { while (0 < (read = input.read(buffer, 0, 12 * 1024))) { output.write(buffer, 0, read); }//from ww w.j av a 2 s.c om output.flush(); }
From source file:gov.nasa.arc.geocam.geocam.HttpPost.java
protected static void assembleMultipart(DataOutputStream out, Map<String, String> vars, String fileKey, String fileName, InputStream istream) throws IOException { for (String key : vars.keySet()) { out.writeBytes("--" + BOUNDARY + CRLF); out.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + CRLF); out.writeBytes(CRLF);/*from ww w . j a va 2 s .c o m*/ out.writeBytes(vars.get(key) + CRLF); } out.writeBytes("--" + BOUNDARY + CRLF); out.writeBytes( "Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + fileName + "\"" + CRLF); out.writeBytes("Content-Type: application/octet-stream" + CRLF); out.writeBytes(CRLF); // write stream //http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html int maxBufferSize = 1024; int bytesAvailable = istream.available(); int bufferSize = Math.min(maxBufferSize, bytesAvailable); byte[] buffer = new byte[bufferSize]; int bytesRead = istream.read(buffer, 0, bufferSize); while (bytesRead > 0) { out.write(buffer, 0, bufferSize); out.flush(); bytesAvailable = istream.available(); bufferSize = Math.min(maxBufferSize, bytesAvailable); bytesRead = istream.read(buffer, 0, bufferSize); } out.writeBytes(CRLF); out.writeBytes("--" + BOUNDARY + "--" + CRLF); out.writeBytes(CRLF); }
From source file:org.apache.hadoop.hdfs.protocol.datatransfer.PacketReceiver.java
/** * Rewrite the last-read packet on the wire to the given output stream. *//*from w w w . j a va2 s. co m*/ public void mirrorPacketTo(DataOutputStream mirrorOut) throws IOException { Preconditions.checkState(!useDirectBuffers, "Currently only supported for non-direct buffers"); mirrorOut.write(curPacketBuf.array(), curPacketBuf.arrayOffset(), curPacketBuf.remaining()); }
From source file:org.b5chat.crossfire.web.FaviconServlet.java
private byte[] getImage(String url) { try {//w w w . ja v a2 s .c o m // Try to get the fiveicon from the url using an HTTP connection from the pool // that also allows to configure timeout values (e.g. connect and get data) GetMethod get = new GetMethod(url); get.setFollowRedirects(true); int response = client.executeMethod(get); if (response < 400) { // Check that the response was successful. Should we also filter 30* code? return get.getResponseBody(); } else { // Remote server returned an error so return null return null; } } catch (IllegalStateException e) { // Something failed (probably a method not supported) so try the old stye now try { URLConnection urlConnection = new URL(url).openConnection(); urlConnection.setReadTimeout(1000); urlConnection.connect(); DataInputStream di = new DataInputStream(urlConnection.getInputStream()); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteStream); int len; byte[] b = new byte[1024]; while ((len = di.read(b)) != -1) { out.write(b, 0, len); } di.close(); out.flush(); return byteStream.toByteArray(); } catch (IOException ioe) { // We failed again so return null return null; } } catch (IOException ioe) { // We failed so return null return null; } }
From source file:com.tonchidot.nfc_contact_exchanger.lib.PictureUploader.java
private String doFileUploadJson(String url, String fileParamName, byte[] data) { try {/* w ww .j ava 2 s . co m*/ String boundary = "BOUNDARY" + new Date().getTime() + "BOUNDARY"; String lineEnd = "\r\n"; String twoHyphens = "--"; URL connUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + fileParamName + "\";filename=\"photo.jpg\"" + lineEnd); dos.writeBytes(lineEnd); dos.write(data, 0, data.length); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder result = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); } catch (IOException e) { if (Config.LOGD) { Log.d(TAG, "IOException : " + e); } } return null; }
From source file:fi.elfcloud.sci.Connection.java
/** * Does a store operation to elfcloud.fi server. * @param headers request headers to be applied * @param dataChunk data to be sent/* ww w. j ava2 s. c o m*/ * @param len length of the data * @throws ECException */ public void sendData(Map<String, String> headers, byte[] dataChunk, int len) throws ECException { Object[] keys = headers.keySet().toArray(); URL url; try { url = new URL(Connection.url + Connection.apiVersion + "/store"); conn = (HttpURLConnection) url.openConnection(); for (int i = 0; i < keys.length; i++) { conn.setRequestProperty((String) keys[i], headers.get(keys[i])); } conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.write(dataChunk, 0, len); String result = conn.getHeaderField("X-ELFCLOUD-RESULT"); wr.close(); if (conn.getResponseCode() != 200 || !result.equals("OK")) { ECDataItemException exception; if (conn.getResponseCode() == 200) { String[] exceptionData = result.split(" ", 4); int exceptionID = Integer.parseInt(exceptionData[1]); String message = exceptionData[3]; if (exceptionID == 101) { cookieJar.removeAll(); while (authTries < 3) { if (auth()) { sendData(headers, dataChunk, len); return; } } authTries = 0; } exception = new ECDataItemException(); exception.setId(exceptionID); exception.setMessage(message); } else { exception = new ECDataItemException(); exception.setId(conn.getResponseCode()); exception.setMessage(conn.getResponseMessage()); } throw exception; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { ECClientException exception = new ECClientException(); exception.setId(408); exception.setMessage("Error connecting to elfcloud.fi server"); authTries = 0; throw exception; } }
From source file:gov.nasa.arc.geocam.geocam.HttpPost.java
protected static void assembleMultipartFiles(DataOutputStream out, Map<String, String> vars, Map<String, File> files) throws IOException { for (String key : vars.keySet()) { out.writeBytes("--" + BOUNDARY + CRLF); out.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + CRLF); out.writeBytes(CRLF);//from w w w . ja va 2 s . c om out.writeBytes(vars.get(key) + CRLF); } for (String key : files.keySet()) { File f = files.get(key); out.writeBytes("--" + BOUNDARY + CRLF); out.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + f.getName() + "\"" + CRLF); out.writeBytes("Content-Type: application/octet-stream" + CRLF); out.writeBytes(CRLF); // write file int maxBufferSize = 1024; FileInputStream fin = new FileInputStream(f); int bytesAvailable = fin.available(); int bufferSize = Math.min(maxBufferSize, bytesAvailable); byte[] buffer = new byte[bufferSize]; int bytesRead = fin.read(buffer, 0, bufferSize); while (bytesRead > 0) { out.write(buffer, 0, bufferSize); bytesAvailable = fin.available(); bufferSize = Math.min(maxBufferSize, bytesAvailable); bytesRead = fin.read(buffer, 0, bufferSize); } } out.writeBytes(CRLF); out.writeBytes("--" + BOUNDARY + "--" + CRLF); out.writeBytes(CRLF); }
From source file:org.apache.fontbox.ttf.TTFSubFont.java
private static long writeTableHeader(DataOutputStream dos, String tag, long offset, byte[] bytes) throws IOException { int n = bytes.length; int nup;/*from ww w . java 2 s .co m*/ long checksum = 0L; for (nup = 0; nup < n; ++nup) { checksum += (((long) bytes[nup]) & 0xffL) << (24 - (nup % 4) * 8); } checksum &= 0xffffffffL; LOG.debug(String.format("Writing table header [%s,%08x,%08x,%08x]", tag, checksum, offset, bytes.length)); byte[] tagbytes = tag.getBytes("US-ASCII"); dos.write(tagbytes, 0, 4); dos.writeInt((int) checksum); dos.writeInt((int) offset); dos.writeInt(bytes.length); // account for the checksum twice, one time for the header field, on time for the content itself. return buildUint32(tagbytes) + checksum + checksum + offset + bytes.length; }