List of usage examples for java.io DataInputStream close
public void close() throws IOException
From source file:com.mellanox.r4h.DataXceiverBase.java
private void processOPRHeaderRequest(Msg msg) throws IOException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException { if (serverSession.getIsClosing()) { LOG.warn("Process OPRHeaderRequest for a closed session, discarding..."); return;//from w w w.ja v a2s . c om } if (LOG.isTraceEnabled()) { LOG.trace("Processing block header request. uri=" + DataXceiverBase.this.uri); } msg.getIn().position(0); DataInputStream in = new DataInputStream(new ByteBufferInputStream(msg.getIn())); final short version = in.readShort(); if (version != DataTransferProtocol.DATA_TRANSFER_VERSION) { in.close(); throw new IOException("Version Mismatch (Expected: " + DataTransferProtocol.DATA_TRANSFER_VERSION + ", Received: " + version + " )"); } Op op = Op.read(in); if (op != Op.WRITE_BLOCK) { throw new IOException("Unknown op " + op + " in data stream"); } parseOpWriteBlock(in); // check single target for transfer-RBW/Finalized if (oprHeader.isTransfer() && oprHeader.getTargets().length > 0) { throw new IOException(oprHeader.getStage() + " does not support multiple targets " + Arrays.asList(oprHeader.getTargets())); } if (LOG.isDebugEnabled()) { LOG.debug("uri= " + DataXceiverBase.this.uri + "\nopWriteBlock: stage=" + oprHeader.getStage() + ", clientname=" + oprHeader.getClientName() + "\n block =" + oprHeader.getBlock() + ", newGs=" + oprHeader.getLatestGenerationStamp() + ", bytesRcvd=[" + oprHeader.getMinBytesRcvd() + ", " + oprHeader.getMaxBytesRcvd() + "]" + "\n targets=" + Arrays.asList(oprHeader.getTargets()) + "; pipelineSize=" + oprHeader.getPipelineSize() + ", srcDataNode=" + oprHeader.getSrcDataNode() + ", isDatanode=" + oprHeader.isDatanode() + ", isClient=" + oprHeader.isClient() + ", isTransfer=" + oprHeader.isTransfer() + ", writeBlock receive buf size " + msg.getIn().limit()); } // We later mutate block's generation stamp and length, but we need to // forward the original version of the block to downstream mirrors, so // make a copy here. final ExtendedBlock originalBlock = new ExtendedBlock(oprHeader.getBlock()); oprHeader.getBlock().setNumBytes(dnBridge.getEstimateBlockSize()); LOG.info("Receiving " + oprHeader.getBlock() + " src: " + uri); boolean isTokenAccessOk = checkAccess(oprHeader.isClient(), oprHeader.getBlock(), oprHeader.getBlockToken(), Op.WRITE_BLOCK, BlockTokenSecretManager.AccessMode.WRITE, msg); if (isTokenAccessOk) { if (oprHeader.isDatanode() || oprHeader.getStage() != BlockConstructionStage.PIPELINE_CLOSE_RECOVERY) { // open a block receiver createBlockReciver(in, NUM_OF_BLOCK_RECEIVER_CREATION_ATTEMPTS); if (LOG.isTraceEnabled()) { LOG.trace("After BlockReceiver creation: " + blockReceiver); } } else { dnBridge.recoverClose(oprHeader.getBlock(), oprHeader.getLatestGenerationStamp(), oprHeader.getMinBytesRcvd()); } // // Connect to downstream machine, if appropriate // if (hasPipeline()) { try { blockReceiver.setMirrorOut(new DummyDataOutputStream()); // we send to pipeline with RDMA and then keep using vanila's // original // receivePacket function by modifying mirror stream with dummy // stream to // avoid sending to pipeline from vanila's flow // openPipelineConnection(); // sendOprHeaderToPipeline(msg, originalBlock); ClientSession.Callbacks csCBs = DataXceiverBase.this.new CSCallbacks(); String clientURI = DataXceiverBase.this.uri.toString(); spw.queueAsyncPipelineConnection(csCBs, clientURI, oprHeader, DataXceiverBase.this); /* queue request to send OPR Header to pipeline */ Msg mirror = msg.getMirror(false); mirror.getOut().clear(); DataOutputStream mirrorOut = new DataOutputStream(new ByteBufferOutputStream(mirror.getOut())); senderWriteBlock(mirrorOut, originalBlock); mirrorOut.flush(); spw.queueAsyncRequest(mirror, this); } catch (Exception e) { if (oprHeader.isClient()) { replyHeaderAck(msg, ERROR, oprHeader.getTargetByIndex(0).getXferAddr()); // NB: Unconditionally using the xfer addr w/o hostname LOG.error(dnBridge.getDN() + ":Exception transfering block " + oprHeader.getBlock() + " to mirror " + oprHeader.getTargetByIndex(0).getInfoAddr() + ": " + StringUtils.stringifyException(e)); } else { LOG.info(dnBridge.getDN() + ":Exception transfering " + oprHeader.getBlock() + " to mirror " + oprHeader.getTargetByIndex(0).getInfoAddr() + "- continuing without the mirror", e); } } } else if (oprHeader.isClient() && !oprHeader.isTransfer()) { replyHeaderAck(msg); // async } } }
From source file:com.momock.http.HttpSession.java
void readHeaders() { try {/*from ww w. j a v a 2 s . c o m*/ if (!fileInfo.exists() || fileInfo.length() == 0) return; DataInputStream din = new DataInputStream(new FileInputStream(fileInfo)); headers = new TreeMap<String, List<String>>(); int headerCount = din.readInt(); for (int i = 0; i < headerCount; i++) { String key = din.readUTF(); int count = din.readInt(); List<String> vals = new ArrayList<String>(); for (int j = 0; j < count; j++) { String val = din.readUTF(); vals.add(val); } headers.put(key, vals); } din.close(); } catch (IOException e) { Logger.error(e); } }
From source file:org.apache.accumulo.core.client.mock.MockTableOperations.java
@Override public void importDirectory(String tableName, String dir, String failureDir, boolean setTime) throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException { long time = System.currentTimeMillis(); MockTable table = acu.tables.get(tableName); if (table == null) { throw new TableNotFoundException(null, tableName, "The table was not found"); }/*w ww . j ava2 s . c om*/ Path importPath = new Path(dir); Path failurePath = new Path(failureDir); FileSystem fs = acu.getFileSystem(); /* * check preconditions */ // directories are directories if (fs.isFile(importPath)) { throw new IOException("Import path must be a directory."); } if (fs.isFile(failurePath)) { throw new IOException("Failure path must be a directory."); } // failures are writable Path createPath = failurePath.suffix("/.createFile"); FSDataOutputStream createStream = null; try { createStream = fs.create(createPath); } catch (IOException e) { throw new IOException("Error path is not writable."); } finally { if (createStream != null) { createStream.close(); } } fs.delete(createPath, false); // failures are empty FileStatus[] failureChildStats = fs.listStatus(failurePath); if (failureChildStats.length > 0) { throw new IOException("Error path must be empty."); } /* * Begin the import - iterate the files in the path */ for (FileStatus importStatus : fs.listStatus(importPath)) { try { FileSKVIterator importIterator = FileOperations.getInstance().newReaderBuilder() .forFile(importStatus.getPath().toString(), fs, fs.getConf()) .withTableConfiguration(AccumuloConfiguration.getDefaultConfiguration()).seekToBeginning() .build(); while (importIterator.hasTop()) { Key key = importIterator.getTopKey(); Value value = importIterator.getTopValue(); if (setTime) { key.setTimestamp(time); } Mutation mutation = new Mutation(key.getRow()); if (!key.isDeleted()) { mutation.put(key.getColumnFamily(), key.getColumnQualifier(), new ColumnVisibility(key.getColumnVisibilityData().toArray()), key.getTimestamp(), value); } else { mutation.putDelete(key.getColumnFamily(), key.getColumnQualifier(), new ColumnVisibility(key.getColumnVisibilityData().toArray()), key.getTimestamp()); } table.addMutation(mutation); importIterator.next(); } } catch (Exception e) { FSDataOutputStream failureWriter = null; DataInputStream failureReader = null; try { failureWriter = fs.create(failurePath.suffix("/" + importStatus.getPath().getName())); failureReader = fs.open(importStatus.getPath()); int read = 0; byte[] buffer = new byte[1024]; while (-1 != (read = failureReader.read(buffer))) { failureWriter.write(buffer, 0, read); } } finally { if (failureReader != null) failureReader.close(); if (failureWriter != null) failureWriter.close(); } } fs.delete(importStatus.getPath(), true); } }
From source file:org.apache.jackrabbit.core.persistence.mem.InMemBundlePersistenceManager.java
/** * Reads the content of the hash maps from the file system * * @throws Exception if an error occurs/*from w ww . j a va2s.c o m*/ */ public synchronized void loadContents() throws Exception { // read item states FileSystemResource fsRes = new FileSystemResource(wspFS, BUNDLE_FILE_PATH); if (!fsRes.exists()) { return; } BufferedInputStream bis = new BufferedInputStream(fsRes.getInputStream()); DataInputStream in = new DataInputStream(bis); try { int n = in.readInt(); // number of entries while (n-- > 0) { String s = in.readUTF(); // id NodeId id = NodeId.valueOf(s); int length = in.readInt(); // data length byte[] data = new byte[length]; in.readFully(data); // data // store in map bundleStore.put(id, data); } } finally { in.close(); } // read references fsRes = new FileSystemResource(wspFS, REFS_FILE_PATH); bis = new BufferedInputStream(fsRes.getInputStream()); in = new DataInputStream(bis); try { int n = in.readInt(); // number of entries while (n-- > 0) { String s = in.readUTF(); // target id NodeId id = NodeId.valueOf(s); int length = in.readInt(); // data length byte[] data = new byte[length]; in.readFully(data); // data // store in map refsStore.put(id, data); } } finally { in.close(); } if (!useFileBlobStore) { // read blobs fsRes = new FileSystemResource(wspFS, BLOBS_FILE_PATH); bis = new BufferedInputStream(fsRes.getInputStream()); in = new DataInputStream(bis); try { int n = in.readInt(); // number of entries while (n-- > 0) { String id = in.readUTF(); // id int length = in.readInt(); // data length byte[] data = new byte[length]; in.readFully(data); // data // store in map blobs.put(id, data); } } finally { in.close(); } } }
From source file:com.lenovo.h2000.services.LicenseServiceImpl.java
@Override public String upload(byte[] fileBytes, Map<String, String> headers) throws Exception { Map<String, String> params = new HashMap<String, String>(); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost( HttpConfig.parseBaseUrl(headers) + "license/import" + "?" + TypeUtil.mapToString(params)); String responseBody = ""; DataInputStream in = null; try {//from w w w .jav a 2 s . c o m ByteArrayEntity requestEntity = new ByteArrayEntity(fileBytes); requestEntity.setContentEncoding("UTF-8"); requestEntity.setContentType("application/octet-stream"); httpPost.setEntity(requestEntity); HttpResponse response = httpClient.execute(httpPost); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); HttpEntity responseEntity = response.getEntity(); responseBody = EntityUtils.toString(responseEntity); } catch (ClientProtocolException e) { e.printStackTrace(); _logger.error("throwing HttpClientUtil.doPost ClientProtocolException with " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); _logger.error("throwing HttpClientUtil.doPost IOException with " + e.getMessage()); } finally { httpClient.close(); if (in != null) { in.close(); } } return responseBody; }
From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java
private void testKerberosDelegationTokenAuthenticator(final boolean doAs) throws Exception { final String doAsUser = doAs ? OK_USER : null; // setting hadoop security to kerberos org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration(); conf.set("hadoop.security.authentication", "kerberos"); UserGroupInformation.setConfiguration(conf); File testDir = new File("target/" + UUID.randomUUID().toString()); Assert.assertTrue(testDir.mkdirs()); MiniKdc kdc = new MiniKdc(MiniKdc.createConf(), testDir); final Server jetty = createJettyServer(); Context context = new Context(); context.setContextPath("/foo"); jetty.setHandler(context);//from w w w . ja va2 s.c o m ((AbstractConnector) jetty.getConnectors()[0]).setResolveNames(true); context.addFilter(new FilterHolder(KDTAFilter.class), "/*", 0); context.addServlet(new ServletHolder(UserServlet.class), "/bar"); try { kdc.start(); File keytabFile = new File(testDir, "test.keytab"); kdc.createPrincipal(keytabFile, "client", "HTTP/localhost"); KDTAFilter.keytabFile = keytabFile.getAbsolutePath(); jetty.start(); final DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token(); final DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL(); final URL url = new URL(getJettyURL() + "/foo/bar"); try { aUrl.getDelegationToken(url, token, FOO_USER, doAsUser); Assert.fail(); } catch (AuthenticationException ex) { Assert.assertTrue(ex.getMessage().contains("GSSException")); } doAsKerberosUser("client", keytabFile.getAbsolutePath(), new Callable<Void>() { @Override public Void call() throws Exception { aUrl.getDelegationToken(url, token, doAs ? doAsUser : "client", doAsUser); Assert.assertNotNull(token.getDelegationToken()); Assert.assertEquals(new Text("token-kind"), token.getDelegationToken().getKind()); // Make sure the token belongs to the right owner ByteArrayInputStream buf = new ByteArrayInputStream(token.getDelegationToken().getIdentifier()); DataInputStream dis = new DataInputStream(buf); DelegationTokenIdentifier id = new DelegationTokenIdentifier(new Text("token-kind")); id.readFields(dis); dis.close(); Assert.assertEquals(doAs ? new Text(OK_USER) : new Text("client"), id.getOwner()); if (doAs) { Assert.assertEquals(new Text("client"), id.getRealUser()); } aUrl.renewDelegationToken(url, token, doAsUser); Assert.assertNotNull(token.getDelegationToken()); aUrl.getDelegationToken(url, token, FOO_USER, doAsUser); Assert.assertNotNull(token.getDelegationToken()); try { aUrl.renewDelegationToken(url, token, doAsUser); Assert.fail(); } catch (Exception ex) { Assert.assertTrue(ex.getMessage().contains("403")); } aUrl.getDelegationToken(url, token, FOO_USER, doAsUser); aUrl.cancelDelegationToken(url, token, doAsUser); Assert.assertNull(token.getDelegationToken()); return null; } }); } finally { jetty.stop(); kdc.stop(); } }
From source file:com.bruce.study.demo.studydata.demos60.httpclient.MyHttpClientActivity.java
/** * ???//from w ww . j a v a 2 s. c o m */ private void getNetInfo() { DataInputStream dataInputStream = null; try { URL url = new URL(ADDRESS); // ?? URLConnection urlConnection = url.openConnection(); // http // ?? dataInputStream = new DataInputStream(urlConnection.getInputStream()); // ?? // DataOutputStream dataOutputStream = new DataOutputStream(urlConnection.getOutputStream()) // ??? int temp = 0; byteArrayBuffer = new ByteArrayBuffer(1000); while ((temp = dataInputStream.read()) != -1) { byteArrayBuffer.append(temp); } // ?? MyHttpClientActivity.this.sendUIMessageEmpty(0x02); } catch (MalformedURLException e) { logE(e.toString()); } catch (IOException e) { logE(e.toString()); } finally { try { if (dataInputStream != null) { dataInputStream.close(); } } catch (IOException e) { logE(e.toString()); } } }
From source file:cit360.sandbox.BackEndMenu.java
public static final void smtpExample() { // declaration section: // smtpClient: our client socket // os: output stream // is: input stream Socket smtpSocket = null;//from w w w .j a va 2 s .c o m DataOutputStream os = null; DataInputStream is = null; // Initialization section: // Try to open a socket on port 25 // Try to open input and output streams try { smtpSocket = new Socket("localhost", 25); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Did not recognize server: localhost"); } catch (IOException e) { System.err.println("Was not able to open I/O connection to: localhost"); } // If everything has been initialized then we want to write some data // to the socket we have opened a connection to on port 25 if (smtpSocket != null && os != null && is != null) { try { os.writeBytes("HELO\n"); os.writeBytes("MAIL From: david.banks0889@gmail.com\n"); os.writeBytes("RCPT To: david.banks0889@gmail.com\n"); os.writeBytes("DATA\n"); os.writeBytes("From: david.banks0889@gmail.com\n"); os.writeBytes("Subject: TEST\n"); os.writeBytes("Hi there\n"); // message body os.writeBytes("\n.\n"); os.writeBytes("QUIT"); // keep on reading from/to the socket till we receive the "Ok" from SMTP, // once we received that then we want to break. String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); if (responseLine.contains("Ok")) { break; } } // clean up: // close the output stream // close the input stream // close the socket os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } }
From source file:net.tuples.doitfx.connector.crypto.PasswordManager.java
private byte[] loadPasscode(final String pSvcPair) { final FileInputStream passCodeFileInStm; final DataInputStream passCodeDataInStm; final byte[] bufferByteArray = new byte[64]; final byte[] decodedByteArray; final int MAX_BYTES_AT_ONCE = 8; final Path passCodeLocation = PathOptions.getDefaultConfigPath().resolve(pSvcPair) .resolve(passcodeFilename);//from ww w.ja va2 s. c om try { passCodeFileInStm = new FileInputStream(passCodeLocation.toFile()); passCodeDataInStm = new DataInputStream(passCodeFileInStm); int counts = 0; int size = 0; while (true) { size = passCodeDataInStm.read(bufferByteArray, counts, MAX_BYTES_AT_ONCE); if (size <= 0) { break; } counts += size; size = 0; } passCodeDataInStm.close(); passCodeFileInStm.close(); decodedByteArray = Base64.decodeBase64(bufferByteArray); return decodedByteArray; } catch (IOException ex) { Logger.getLogger(PasswordManager.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:org.apache.hadoop.hdfs.server.namenode.AvatarNodeNew.java
/** * Reads the timestamp of the last checkpoint from the remote fstime file. *//*from w ww. j a v a2 s. c o m*/ static long readRemoteFstime(Configuration conf) throws IOException { String edit = null; if (instance == InstanceId.NODEZERO) { edit = conf.get("dfs.name.edits.dir.shared1"); } else if (instance == InstanceId.NODEONE) { edit = conf.get("dfs.name.edits.dir.shared0"); } else { LOG.info("Instance is invalid. " + instance); throw new IOException("Instance is invalid. " + instance); } File timeFile = new File(edit + TIMEFILE); long timeStamp = 0L; DataInputStream in = null; try { in = new DataInputStream(new FileInputStream(timeFile)); timeStamp = in.readLong(); } catch (IOException e) { if (!timeFile.exists()) { String msg = "Error reading checkpoint time file " + timeFile + " file does not exist."; LOG.error(msg); throw new IOException(msg + e); } else if (!timeFile.canRead()) { String msg = "Error reading checkpoint time file " + timeFile + " cannot read file of size " + timeFile.length() + " last modified " + dateForm.format(new Date(timeFile.lastModified())); LOG.error(msg); throw new IOException(msg + e); } else { String msg = "Error reading checkpoint time file " + timeFile; LOG.error(msg); throw new IOException(msg + e); } } finally { if (in != null) { in.close(); } } return timeStamp; }