📄 streaminitiation.java
字号:
package com.valhalla.jbother.jabber.smack;import org.jivesoftware.smack.packet.IQ;import org.jivesoftware.smack.*;import org.jivesoftware.smack.util.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smackx.*;import java.io.*;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Iterator;/** * Created by IntelliJ IDEA. * User: luke * Date: Dec 15, 2004 * Time: 2:36:48 PM * To change this template use Options | File Templates. *//** * represents information passed in <si> (stream initiation) packet */public class StreamInitiation extends IQ{ private static String fId = "$Id$"; public static final String NAMESPACE = "http://jabber.org/protocol/si"; public static final String PROFILE = "http://jabber.org/protocol/si/profile/file-transfer"; private FileDetails fileDetails = null; private Feature feature = null; private String id = null; // constructor public StreamInitiation() { setType(IQ.Type.SET); id = StringUtils.randomString(20); feature = new Feature(); } // getters & setters public FileDetails getFileDetails() { return fileDetails; } public void setFileDetails(FileDetails aFile) { fileDetails = aFile; } public Feature getFeature() { return feature; } public void setFeature(Feature aFeature) { feature = aFeature; } public String getId() { return id; } public void setId(String aID) { id = aID; } public String getChildElementXML() { StringBuffer buf = new StringBuffer(); buf.append("<si xmlns=\"" + NAMESPACE + "\" profile=\"" + PROFILE + "\""); if(id != null) buf.append(" id=\"" + id + "\""); buf.append(">"); if(fileDetails != null) buf.append(fileDetails.getXML()); if(feature != null) buf.append(feature.getXML()); buf.append("</si>"); return buf.toString(); } /** * helper method for creating "confirmation" <si> messages * @return StreamInitiation message of type RESULT */ public StreamInitiation createConfirmationMessage() { // prepare a confirmation <si> message; it should have null stream id // now we're replying, so to=from and vice versa StreamInitiation confirmSI = new StreamInitiation(); confirmSI.setFrom(getTo()); confirmSI.setTo(getFrom()); confirmSI.setType(IQ.Type.RESULT); confirmSI.setPacketID(getPacketID()); Feature feature = new Feature("submit"); FormField formfield = new FormField("stream-method"); formfield.setType(null); formfield.addValue("http://jabber.org/protocol/bytestreams"); feature.getDataForm().addField(formfield); confirmSI.setFeature(feature); confirmSI.setId(null); return confirmSI; } /** * subclass that carry file information */ public static class FileDetails { private File fFile; private String fFileName; private long fFileSize = 0; private String fDescription; private String fHash; private String fDate = DateTimeUtils.getDateTime(); private File fDestFile;// for <range> tag (to be implemented later)// private Long fOffset;// private Long fLength; public FileDetails(File aFile) { fFile = aFile; fFileName = aFile.getName(); fFileSize = aFile.length(); fHash = createMD5Sum(); fDestFile = new File(aFile.getName()); } public FileDetails(String aFilename) throws FileNotFoundException { fFile = new File(aFilename); fFileName = fFile.getName(); fFileSize = fFile.length(); fHash = new String(); fDestFile = new File(aFilename); } public String getFileName() { return fFileName; } public void setFileName(String aFileName) { this.fFileName = aFileName; } public long getFileSize() { return fFileSize; } public void setFileSize(long aFileSize) { this.fFileSize = aFileSize; } public String getDescription() { return fDescription; } public void setDescription(String aDescription) { this.fDescription = aDescription; } public String getHash() { return fHash; } public void setHash(String aHash) { this.fHash = aHash; } public String getDate() { return fDate; } public File getFile() { return fFile; } public void setFile(File aFile) { this.fFile = aFile; } public File getDestFile() { return fDestFile; } public void setDestFile(File fDestFile) { this.fDestFile = fDestFile; } public String getXML() { StringBuffer buf = new StringBuffer(); buf.append("<file xmlns=\"").append(PROFILE).append("\""); if(getFileName() != null) buf.append(" name=\"").append(getFileName()).append("\""); if(getFileSize() != 0) buf.append(" size=\"").append(getFileSize()).append("\""); if(getDate() != null) buf.append(" date=\"").append(getDate()).append("\""); if(getHash() != null) buf.append(" hash=\"").append(getHash()).append("\""); buf.append(">"); if(getDescription() != null) buf.append("<desc>").append(getDescription()).append("</desc>"); buf.append("</file>"); return buf.toString(); } /** * returns MD5 sum of a file */ public String createMD5Sum() { String out = ""; try { byte[] digest; MessageDigest md = MessageDigest.getInstance("MD5"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fFile)); byte[] buff = new byte[1024]; int chunk = 1024; int bytesRead = 0; while((bytesRead=bis.read(buff,0,chunk)) != -1) { md.update(buff,0,bytesRead); } digest = md.digest(); String hexChar = null; if( digest != null) { for(int i=0;i<digest.length;i++) { if(digest[i] > 0) { hexChar = Integer.toHexString(digest[i]); } else if(digest[i] < 0) { hexChar = Integer.toHexString(digest[i]).substring(6); } else { hexChar = "00"; } out += hexChar; } } return out; } catch (FileNotFoundException e) { System.err.println("File not found!"); return null; } catch (NoSuchAlgorithmException e) { System.out.println("SHA1 not supported!"); return null; } catch (IOException e) { e.printStackTrace(); } // else something went wrong return null; } } /** * class that will hold feature information * re-using DataForm code to */ public static class Feature { public static final String BYTESTREAMS_FEATURE = "http://jabber.org/protocol/bytestreams"; DataForm fDataForm; public Feature() { this("form"); FormField field = new FormField("stream-method"); field.setType(FormField.TYPE_LIST_SINGLE); field.addOption(new FormField.Option("http://jabber.org/protocol/bytestreams")); fDataForm.addField(field); } public Feature(String aDataFormType) { fDataForm = new DataForm(aDataFormType); } public void setDataForm(DataForm fFeatureForm) { this.fDataForm = fFeatureForm; } public DataForm getDataForm() { return fDataForm; } public String getXML() { StringBuffer buf = new StringBuffer(); buf.append("<feature xmlns=\"http://jabber.org/protocol/feature-neg\">"); if(fDataForm != null) buf.append(fDataForm.toXML()); buf.append("</feature>"); return buf.toString(); } /** * check if this Feature provides bytestream option * this method works with both "set" and "result" <si> requests, the one in form: * * @return true if this feature provides bytestreams */ public boolean providesBytestreamOption() { // check if the caller asks for bytestream feature Iterator iFields = fDataForm.getFields(); while(iFields.hasNext()) { FormField field = (FormField)iFields.next(); if(field.getVariable().equals("stream-method")) { Iterator iOptions = field.getOptions(); while(iOptions.hasNext()) { FormField.Option option = (FormField.Option)iOptions.next(); if(option.getValue().equals(BYTESTREAMS_FEATURE)) { return true; } } Iterator iValues = field.getValues(); while(iValues.hasNext()) { String value = (String)iValues.next(); if(value.equals(BYTESTREAMS_FEATURE)) { return true; } } } } return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -