List of usage examples for java.io DataOutputStream writeShort
public final void writeShort(int v) throws IOException
short
to the underlying output stream as two bytes, high byte first. From source file:ClassFile.java
public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception { dos.writeShort(ConstantPoolInfo.indexOf(name, pool)); dos.writeInt(data.length);/*w w w . j a v a2 s .c om*/ dos.write(data, 0, data.length); }
From source file:ClassFile.java
/** * Write out a method_info, do constant table fixups on the write. *//* www.j a v a 2 s . c om*/ public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception { dos.writeShort(accessFlags); dos.writeShort(ConstantPoolInfo.indexOf(name, pool)); dos.writeShort(ConstantPoolInfo.indexOf(signature, pool)); if (attributes == null) { dos.writeShort(0); } else { dos.writeShort(attributes.length); for (int i = 0; i < attributes.length; i++) attributes[i].write(dos, pool); } }
From source file:ClassFile.java
public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception { dos.writeShort(accessFlags); dos.writeShort(ConstantPoolInfo.indexOf(name, pool)); dos.writeShort(ConstantPoolInfo.indexOf(signature, pool)); if (attributes == null) { dos.writeShort(0);/*from www . jav a 2s. c o m*/ } else { dos.writeShort(attributes.length); for (int i = 0; i < attributes.length; i++) { attributes[i].write(dos, pool); } } }
From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java
/** * Read a block from the disk.// ww w . j a v a 2s .com * @param in The stream to read from * @throws IOException */ private void readBlock(DataInputStream in) throws IOException { LOG.debug("Mathod called : readBlock()"); // // Read in the header // long blockId = in.readLong(); Block block = new Block(blockId, 0, in.readLong()); long startOffset = in.readLong(); long length = in.readLong(); String clientName = Text.readString(in); // send the block // OutputStream baseStream = NetUtils.getOutputStream(s, datanode.socketWriteTimeout); OutputStream baseStream = s.getOutputStream(); // DataOutputStream out = new DataOutputStream( // new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE)); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(baseStream)); BlockSender blockSender = null; final String clientTraceFmt = clientName.length() > 0 && ClientTraceLog.isInfoEnabled() ? String.format(DN_CLIENTTRACE_FORMAT, localAddress, remoteAddress, "%d", "HDFS_READ", clientName, datanode.dnRegistration.getStorageID(), block) : datanode.dnRegistration + " Served block " + block + " to " + s.getInetAddress(); try { try { blockSender = new BlockSender(block, startOffset, length, true, true, false, datanode, clientTraceFmt); } catch (IOException e) { out.writeShort(DataTransferProtocol.OP_STATUS_ERROR); throw e; } out.writeShort(DataTransferProtocol.OP_STATUS_SUCCESS); // send op status long read = blockSender.sendBlock(out, baseStream, null); // send data if (blockSender.isBlockReadFully()) { // See if client verification succeeded. // This is an optional response from client. try { if (in.readShort() == DataTransferProtocol.OP_STATUS_CHECKSUM_OK && datanode.blockScanner != null) { datanode.blockScanner.verifiedByClient(block); } } catch (IOException ignored) { } } datanode.myMetrics.bytesRead.inc((int) read); datanode.myMetrics.blocksRead.inc(); } catch (SocketException ignored) { // Its ok for remote side to close the connection anytime. datanode.myMetrics.blocksRead.inc(); } catch (IOException ioe) { /* What exactly should we do here? * Earlier version shutdown() datanode if there is disk error. */ LOG.warn(datanode.dnRegistration + ":Got exception while serving " + block + " to " + s.getInetAddress() + ":\n" + StringUtils.stringifyException(ioe)); throw ioe; } finally { LOG.debug("Finalizing : readBlock()"); IOUtils.closeStream(out); IOUtils.closeStream(blockSender); } }
From source file:org.apache.hadoop.hdfs.server.datanode.DataXceiver.java
/** * Receive a block and write it to disk, it then notifies the namenode to * remove the copy from the source.// w w w.j a v a 2 s. c om * * @param in The stream to read from * @throws IOException */ private void replaceBlock(DataInputStream in) throws IOException { /* read header */ long blockId = in.readLong(); Block block = new Block(blockId, dataXceiverServer.estimateBlockSize, in.readLong()); // block id & generation stamp String sourceID = Text.readString(in); // read del hint DatanodeInfo proxySource = new DatanodeInfo(); // read proxy source proxySource.readFields(in); Token<BlockTokenIdentifier> accessToken = new Token<BlockTokenIdentifier>(); accessToken.readFields(in); if (datanode.isBlockTokenEnabled) { try { datanode.blockTokenSecretManager.checkAccess(accessToken, null, block, BlockTokenSecretManager.AccessMode.REPLACE); } catch (InvalidToken e) { LOG.warn("Invalid access token in request from " + remoteAddress + " for OP_REPLACE_BLOCK for block " + block); sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR_ACCESS_TOKEN, datanode.socketWriteTimeout); return; } } if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start LOG.warn("Not able to receive block " + blockId + " from " + s.getRemoteSocketAddress() + " because threads quota is exceeded."); sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR, datanode.socketWriteTimeout); return; } Socket proxySock = null; DataOutputStream proxyOut = null; short opStatus = DataTransferProtocol.OP_STATUS_SUCCESS; BlockReceiver blockReceiver = null; DataInputStream proxyReply = null; try { // get the output stream to the proxy InetSocketAddress proxyAddr = NetUtils.createSocketAddr(proxySource.getName()); proxySock = datanode.newSocket(); NetUtils.connect(proxySock, proxyAddr, datanode.socketTimeout); proxySock.setSoTimeout(datanode.socketTimeout); OutputStream baseStream = NetUtils.getOutputStream(proxySock, datanode.socketWriteTimeout); proxyOut = new DataOutputStream(new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE)); /* send request to the proxy */ proxyOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION); // transfer version proxyOut.writeByte(DataTransferProtocol.OP_COPY_BLOCK); // op code proxyOut.writeLong(block.getBlockId()); // block id proxyOut.writeLong(block.getGenerationStamp()); // block id accessToken.write(proxyOut); proxyOut.flush(); // receive the response from the proxy proxyReply = new DataInputStream( new BufferedInputStream(NetUtils.getInputStream(proxySock), BUFFER_SIZE)); short status = proxyReply.readShort(); if (status != DataTransferProtocol.OP_STATUS_SUCCESS) { if (status == DataTransferProtocol.OP_STATUS_ERROR_ACCESS_TOKEN) { throw new IOException("Copy block " + block + " from " + proxySock.getRemoteSocketAddress() + " failed due to access token error"); } throw new IOException( "Copy block " + block + " from " + proxySock.getRemoteSocketAddress() + " failed"); } // open a block receiver and check if the block does not exist blockReceiver = new BlockReceiver(block, proxyReply, proxySock.getRemoteSocketAddress().toString(), proxySock.getLocalSocketAddress().toString(), false, "", null, datanode); // receive a block blockReceiver.receiveBlock(null, null, null, null, dataXceiverServer.balanceThrottler, -1); // notify name node datanode.notifyNamenodeReceivedBlock(block, sourceID); LOG.info("Moved block " + block + " from " + s.getRemoteSocketAddress()); } catch (IOException ioe) { opStatus = DataTransferProtocol.OP_STATUS_ERROR; throw ioe; } finally { // receive the last byte that indicates the proxy released its thread resource if (opStatus == DataTransferProtocol.OP_STATUS_SUCCESS) { try { proxyReply.readChar(); } catch (IOException ignored) { } } // now release the thread resource dataXceiverServer.balanceThrottler.release(); // send response back try { sendResponse(s, opStatus, datanode.socketWriteTimeout); } catch (IOException ioe) { LOG.warn("Error writing reply back to " + s.getRemoteSocketAddress()); } IOUtils.closeStream(proxyOut); IOUtils.closeStream(blockReceiver); IOUtils.closeStream(proxyReply); } }
From source file:org.opennms.core.test.dns.DNSServer.java
byte[] doAXFR(final Name name, final Message query, final TSIG tsig, TSIGRecord qtsig, final Socket s) { final Zone zone = m_znames.get(name); boolean first = true; if (zone == null) return errorMessage(query, Rcode.REFUSED); @SuppressWarnings("unchecked") final Iterator<RRset> it = zone.AXFR(); try {/*from w w w . j av a2 s. c o m*/ final DataOutputStream dataOut = new DataOutputStream(s.getOutputStream()); int id = query.getHeader().getID(); while (it.hasNext()) { final RRset rrset = it.next(); final Message response = new Message(id); final Header header = response.getHeader(); header.setFlag(Flags.QR); header.setFlag(Flags.AA); addRRset(rrset.getName(), response, rrset, Section.ANSWER, FLAG_DNSSECOK); if (tsig != null) { tsig.applyStream(response, qtsig, first); qtsig = response.getTSIG(); } first = false; final byte[] out = response.toWire(); dataOut.writeShort(out.length); dataOut.write(out); } } catch (final IOException ex) { LOG.warn("AXFR failed", ex); } try { s.close(); } catch (final IOException ex) { LOG.warn("error closing socket", ex); } return null; }
From source file:ClassFile.java
public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception { dos.write(type);/*ww w . j av a 2 s . c o m*/ switch (type) { case CLASS: case STRING: dos.writeShort(indexOf(arg1, pool)); break; case FIELDREF: case METHODREF: case INTERFACE: case NAMEANDTYPE: dos.writeShort(indexOf(arg1, pool)); dos.writeShort(indexOf(arg2, pool)); break; case INTEGER: dos.writeInt(intValue); break; case FLOAT: dos.writeFloat(floatValue); break; case LONG: dos.writeLong(longValue); break; case DOUBLE: dos.writeDouble(doubleValue); break; case ASCIZ: case UNICODE: dos.writeShort(strValue.length()); dos.writeBytes(strValue); break; default: throw new Exception("ConstantPoolInfo::write() - bad type."); } }
From source file:ClassFile.java
/** * Write the class out as a stream of bytes to the output * stream./* w ww . ja v a2 s.c om*/ * * Generally you will read a class file, manipulate * it in some way, and then write it out again before passing * it to <TT>defineClass</TT> in some class loader. */ public void write(OutputStream out) throws IOException, Exception { DataOutputStream dos = new DataOutputStream(out); if (!isValidClass) { throw new Exception("ClassFile::write() - Invalid Class"); } dos.writeInt(magic); dos.writeShort(majorVersion); dos.writeShort(minorVersion); dos.writeShort(constantPool.length); for (int i = 1; i < constantPool.length; i++) { if (constantPool[i] != null) constantPool[i].write(dos, constantPool); } dos.writeShort(accessFlags); dos.writeShort(ConstantPoolInfo.indexOf(thisClass, constantPool)); dos.writeShort(ConstantPoolInfo.indexOf(superClass, constantPool)); if (interfaces == null) { dos.writeShort(0); } else { dos.writeShort(interfaces.length); for (int i = 0; i < interfaces.length; i++) { dos.writeShort(ConstantPoolInfo.indexOf(interfaces[i], constantPool)); } } if (fields == null) { dos.writeShort(0); } else { dos.writeShort(fields.length); for (int i = 0; i < fields.length; i++) { fields[i].write(dos, constantPool); } } if (methods == null) { dos.writeShort(0); } else { dos.writeShort(methods.length); for (int i = 0; i < methods.length; i++) { methods[i].write(dos, constantPool); } } if (attributes == null) { dos.writeShort(0); } else { dos.writeShort(attributes.length); for (int i = 0; i < attributes.length; i++) { attributes[i].write(dos, constantPool); } } }
From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java
/** * Get block checksum (MD5 of CRC32)./*from www. j av a 2s . c o m*/ * @param in */ void getBlockChecksum(DataInputStream in) throws IOException { LOG.debug("Mathod called : getBlockChecksum()"); final Block block = new Block(in.readLong(), 0, in.readLong()); DataOutputStream out = null; final MetaDataInputStream metadataIn = datanode.data.getMetaDataInputStream(block); final DataInputStream checksumIn = new DataInputStream(new BufferedInputStream(metadataIn, BUFFER_SIZE)); try { //read metadata file final BlockMetadataHeader header = BlockMetadataHeader.readHeader(checksumIn); final DataChecksum checksum = header.getChecksum(); final int bytesPerCRC = checksum.getBytesPerChecksum(); final long crcPerBlock = (metadataIn.getLength() - BlockMetadataHeader.getHeaderSize()) / checksum.getChecksumSize(); //compute block checksum final MD5Hash md5 = MD5Hash.digest(checksumIn); if (LOG.isDebugEnabled()) { LOG.debug("block=" + block + ", bytesPerCRC=" + bytesPerCRC + ", crcPerBlock=" + crcPerBlock + ", md5=" + md5); } //write reply //out = new DataOutputStream( // NetUtils.getOutputStream(s, datanode.socketWriteTimeout)); out = new DataOutputStream(s.getOutputStream()); out.writeShort(DataTransferProtocol.OP_STATUS_SUCCESS); out.writeInt(bytesPerCRC); out.writeLong(crcPerBlock); md5.write(out); out.flush(); } finally { LOG.debug("Finalizing : getBlockChecksum()"); IOUtils.closeStream(out); IOUtils.closeStream(checksumIn); IOUtils.closeStream(metadataIn); } }
From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java
/** * Receive a block and write it to disk, it then notifies the namenode to * remove the copy from the source.//from www . java 2 s . c o m * * @param in The stream to read from * @throws IOException */ private void replaceBlock(DataInputStream in) throws IOException { LOG.debug("Mathod called : replaceBlock()"); /* read header */ long blockId = in.readLong(); Block block = new Block(blockId, dataXceiverServer.estimateBlockSize, in.readLong()); // block id & generation stamp String sourceID = Text.readString(in); // read del hint DatanodeInfo proxySource = new DatanodeInfo(); // read proxy source proxySource.readFields(in); if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start LOG.warn("Not able to receive block " + blockId + " from " + s.getRemoteSocketAddress() + " because threads quota is exceeded."); sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR, datanode.socketWriteTimeout); return; } JxtaSocket proxySock = null; DataOutputStream proxyOut = null; short opStatus = DataTransferProtocol.OP_STATUS_SUCCESS; BlockReceiver blockReceiver = null; DataInputStream proxyReply = null; ReliableOutputStream baseStream = null; ReliableInputStream replyStream = null; try { // get the output stream to the proxy //InetSocketAddress proxyAddr = NetUtils.createSocketAddr( // proxySource.getName()); //proxySock = datanode.newSocket(); proxySock = datanode.getDnPeer().getInfoSocket(proxySource.getPeerId().toString()); // NetUtils.connect(proxySock, proxyAddr, datanode.socketTimeout); // proxySock.setSoTimeout(datanode.socketTimeout); /*OutputStream baseStream = NetUtils.getOutputStream(proxySock, datanode.socketWriteTimeout); proxyOut = new DataOutputStream( new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE)); */ baseStream = (ReliableOutputStream) proxySock.getOutputStream(); proxyOut = new DataOutputStream(new BufferedOutputStream(baseStream)); /* send request to the proxy */ proxyOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION); // transfer version proxyOut.writeByte(DataTransferProtocol.OP_COPY_BLOCK); // op code proxyOut.writeLong(block.getBlockId()); // block id proxyOut.writeLong(block.getGenerationStamp()); // block id proxyOut.flush(); // receive the response from the proxy //proxyReply = new DataInputStream(new BufferedInputStream( // NetUtils.getInputStream(proxySock), BUFFER_SIZE)); replyStream = (ReliableInputStream) proxySock.getInputStream(); proxyReply = new DataInputStream(new BufferedInputStream(replyStream)); // open a block receiver and check if the block does not exist blockReceiver = new BlockReceiver(block, proxyReply, proxySock.getRemoteSocketAddress().toString(), proxySock.getLocalSocketAddress().toString(), false, "", null, datanode); // receive a block blockReceiver.receiveBlock(null, null, null, null, dataXceiverServer.balanceThrottler, -1); // notify name node datanode.notifyNamenodeReceivedBlock(block, sourceID); LOG.info("Moved block " + block + " from " + s.getRemoteSocketAddress()); } catch (IOException ioe) { opStatus = DataTransferProtocol.OP_STATUS_ERROR; throw ioe; } finally { // receive the last byte that indicates the proxy released its thread resource if (opStatus == DataTransferProtocol.OP_STATUS_SUCCESS) { try { proxyReply.readChar(); } catch (IOException ignored) { } } // now release the thread resource dataXceiverServer.balanceThrottler.release(); // send response back try { sendResponse(s, opStatus, datanode.socketWriteTimeout); } catch (IOException ioe) { LOG.warn("Error writing reply back to " + s.getRemoteSocketAddress()); } LOG.debug("Finalizing : replaceBlock()"); LOG.debug("baseStream queue empty : " + baseStream.isQueueEmpty()); IOUtils.closeStream(proxyOut); IOUtils.closeStream(blockReceiver); IOUtils.closeStream(proxyReply); } }