home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / javax / activation / DataHandler.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  6.9 KB  |  284 lines

  1. package javax.activation;
  2.  
  3. import java.awt.datatransfer.DataFlavor;
  4. import java.awt.datatransfer.Transferable;
  5. import java.awt.datatransfer.UnsupportedFlavorException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.io.PipedInputStream;
  10. import java.io.PipedOutputStream;
  11. import java.net.URL;
  12.  
  13. public class DataHandler implements Transferable {
  14.    private DataSource dataSource = null;
  15.    private DataSource objDataSource = null;
  16.    private Object object = null;
  17.    private String objectMimeType = null;
  18.    private CommandMap currentCommandMap = null;
  19.    private static final DataFlavor[] emptyFlavors = new DataFlavor[0];
  20.    private DataFlavor[] transferFlavors;
  21.    private DataContentHandler dataContentHandler;
  22.    private DataContentHandler factoryDCH;
  23.    private static DataContentHandlerFactory factory = null;
  24.    private DataContentHandlerFactory oldFactory;
  25.    private String shortType;
  26.  
  27.    public DataHandler(DataSource var1) {
  28.       this.transferFlavors = emptyFlavors;
  29.       this.dataContentHandler = null;
  30.       this.factoryDCH = null;
  31.       this.oldFactory = null;
  32.       this.shortType = null;
  33.       this.dataSource = var1;
  34.       this.oldFactory = factory;
  35.    }
  36.  
  37.    public DataHandler(Object var1, String var2) {
  38.       this.transferFlavors = emptyFlavors;
  39.       this.dataContentHandler = null;
  40.       this.factoryDCH = null;
  41.       this.oldFactory = null;
  42.       this.shortType = null;
  43.       this.object = var1;
  44.       this.objectMimeType = var2;
  45.       this.oldFactory = factory;
  46.    }
  47.  
  48.    public DataHandler(URL var1) {
  49.       this.transferFlavors = emptyFlavors;
  50.       this.dataContentHandler = null;
  51.       this.factoryDCH = null;
  52.       this.oldFactory = null;
  53.       this.shortType = null;
  54.       this.dataSource = new URLDataSource(var1);
  55.       this.oldFactory = factory;
  56.    }
  57.  
  58.    private synchronized CommandMap getCommandMap() {
  59.       return this.currentCommandMap != null ? this.currentCommandMap : CommandMap.getDefaultCommandMap();
  60.    }
  61.  
  62.    public DataSource getDataSource() {
  63.       if (this.dataSource == null) {
  64.          if (this.objDataSource == null) {
  65.             this.objDataSource = new DataHandlerDataSource(this);
  66.          }
  67.  
  68.          return this.objDataSource;
  69.       } else {
  70.          return this.dataSource;
  71.       }
  72.    }
  73.  
  74.    public String getName() {
  75.       return this.dataSource != null ? this.dataSource.getName() : null;
  76.    }
  77.  
  78.    public String getContentType() {
  79.       return this.dataSource != null ? this.dataSource.getContentType() : this.objectMimeType;
  80.    }
  81.  
  82.    public InputStream getInputStream() throws IOException {
  83.       Object var1 = null;
  84.       if (this.dataSource != null) {
  85.          var1 = this.dataSource.getInputStream();
  86.       } else {
  87.          DataContentHandler var2 = this.getDataContentHandler();
  88.          if (var2 == null) {
  89.             throw new UnsupportedDataTypeException("no DCH for MIME type " + this.getBaseType());
  90.          }
  91.  
  92.          if (var2 instanceof ObjectDataContentHandler && ((ObjectDataContentHandler)var2).getDCH() == null) {
  93.             throw new UnsupportedDataTypeException("no object DCH for MIME type " + this.getBaseType());
  94.          }
  95.  
  96.          PipedOutputStream var4 = new PipedOutputStream();
  97.          PipedInputStream var5 = new PipedInputStream(var4);
  98.          (new Thread(new 1(this, var2, var4), "DataHandler.getInputStream")).start();
  99.          var1 = var5;
  100.       }
  101.  
  102.       return (InputStream)var1;
  103.    }
  104.  
  105.    public void writeTo(OutputStream var1) throws IOException {
  106.       if (this.dataSource != null) {
  107.          Object var2 = null;
  108.          byte[] var3 = new byte[8192];
  109.          InputStream var8 = this.dataSource.getInputStream();
  110.  
  111.          int var4;
  112.          try {
  113.             while((var4 = var8.read(var3)) > 0) {
  114.                var1.write(var3, 0, var4);
  115.             }
  116.          } finally {
  117.             var8.close();
  118.             Object var9 = null;
  119.          }
  120.       } else {
  121.          DataContentHandler var10 = this.getDataContentHandler();
  122.          var10.writeTo(this.object, this.objectMimeType, var1);
  123.       }
  124.  
  125.    }
  126.  
  127.    public OutputStream getOutputStream() throws IOException {
  128.       return this.dataSource != null ? this.dataSource.getOutputStream() : null;
  129.    }
  130.  
  131.    public synchronized DataFlavor[] getTransferDataFlavors() {
  132.       if (factory != this.oldFactory) {
  133.          this.transferFlavors = emptyFlavors;
  134.       }
  135.  
  136.       if (this.transferFlavors == emptyFlavors) {
  137.          this.transferFlavors = this.getDataContentHandler().getTransferDataFlavors();
  138.       }
  139.  
  140.       return this.transferFlavors;
  141.    }
  142.  
  143.    public boolean isDataFlavorSupported(DataFlavor var1) {
  144.       DataFlavor[] var2 = this.getTransferDataFlavors();
  145.  
  146.       for(int var3 = 0; var3 < var2.length; ++var3) {
  147.          if (var2[var3].equals(var1)) {
  148.             return true;
  149.          }
  150.       }
  151.  
  152.       return false;
  153.    }
  154.  
  155.    public Object getTransferData(DataFlavor var1) throws UnsupportedFlavorException, IOException {
  156.       return this.getDataContentHandler().getTransferData(var1, this.dataSource);
  157.    }
  158.  
  159.    public synchronized void setCommandMap(CommandMap var1) {
  160.       if (var1 != this.currentCommandMap || var1 == null) {
  161.          this.transferFlavors = emptyFlavors;
  162.          this.dataContentHandler = null;
  163.          this.currentCommandMap = var1;
  164.       }
  165.  
  166.    }
  167.  
  168.    public CommandInfo[] getPreferredCommands() {
  169.       return this.dataSource != null ? this.getCommandMap().getPreferredCommands(this.getBaseType(), this.dataSource) : this.getCommandMap().getPreferredCommands(this.getBaseType());
  170.    }
  171.  
  172.    public CommandInfo[] getAllCommands() {
  173.       return this.dataSource != null ? this.getCommandMap().getAllCommands(this.getBaseType(), this.dataSource) : this.getCommandMap().getAllCommands(this.getBaseType());
  174.    }
  175.  
  176.    public CommandInfo getCommand(String var1) {
  177.       return this.dataSource != null ? this.getCommandMap().getCommand(this.getBaseType(), var1, this.dataSource) : this.getCommandMap().getCommand(this.getBaseType(), var1);
  178.    }
  179.  
  180.    public Object getContent() throws IOException {
  181.       return this.object != null ? this.object : this.getDataContentHandler().getContent(this.getDataSource());
  182.    }
  183.  
  184.    public Object getBean(CommandInfo var1) {
  185.       Object var2 = null;
  186.  
  187.       try {
  188.          Object var3 = null;
  189.          ClassLoader var6 = SecuritySupport.getContextClassLoader();
  190.          if (var6 == null) {
  191.             var6 = this.getClass().getClassLoader();
  192.          }
  193.  
  194.          var2 = var1.getCommandObject(this, var6);
  195.       } catch (IOException var4) {
  196.       } catch (ClassNotFoundException var5) {
  197.       }
  198.  
  199.       return var2;
  200.    }
  201.  
  202.    private synchronized DataContentHandler getDataContentHandler() {
  203.       if (factory != this.oldFactory) {
  204.          this.oldFactory = factory;
  205.          this.factoryDCH = null;
  206.          this.dataContentHandler = null;
  207.          this.transferFlavors = emptyFlavors;
  208.       }
  209.  
  210.       if (this.dataContentHandler != null) {
  211.          return this.dataContentHandler;
  212.       } else {
  213.          String var1 = this.getBaseType();
  214.          if (this.factoryDCH == null && factory != null) {
  215.             this.factoryDCH = factory.createDataContentHandler(var1);
  216.          }
  217.  
  218.          if (this.factoryDCH != null) {
  219.             this.dataContentHandler = this.factoryDCH;
  220.          }
  221.  
  222.          if (this.dataContentHandler == null) {
  223.             if (this.dataSource != null) {
  224.                this.dataContentHandler = this.getCommandMap().createDataContentHandler(var1, this.dataSource);
  225.             } else {
  226.                this.dataContentHandler = this.getCommandMap().createDataContentHandler(var1);
  227.             }
  228.          }
  229.  
  230.          if (this.dataSource != null) {
  231.             this.dataContentHandler = new DataSourceDataContentHandler(this.dataContentHandler, this.dataSource);
  232.          } else {
  233.             this.dataContentHandler = new ObjectDataContentHandler(this.dataContentHandler, this.object, this.objectMimeType);
  234.          }
  235.  
  236.          return this.dataContentHandler;
  237.       }
  238.    }
  239.  
  240.    private synchronized String getBaseType() {
  241.       if (this.shortType == null) {
  242.          String var1 = this.getContentType();
  243.  
  244.          try {
  245.             MimeType var2 = new MimeType(var1);
  246.             this.shortType = var2.getBaseType();
  247.          } catch (MimeTypeParseException var3) {
  248.             this.shortType = var1;
  249.          }
  250.       }
  251.  
  252.       return this.shortType;
  253.    }
  254.  
  255.    public static synchronized void setDataContentHandlerFactory(DataContentHandlerFactory var0) {
  256.       if (factory != null) {
  257.          throw new Error("DataContentHandlerFactory already defined");
  258.       } else {
  259.          SecurityManager var1 = System.getSecurityManager();
  260.          if (var1 != null) {
  261.             try {
  262.                var1.checkSetFactory();
  263.             } catch (SecurityException var3) {
  264.                if (DataHandler.class.getClassLoader() != var0.getClass().getClassLoader()) {
  265.                   throw var3;
  266.                }
  267.             }
  268.          }
  269.  
  270.          factory = var0;
  271.       }
  272.    }
  273.  
  274.    // $FF: synthetic method
  275.    static Object access$000(DataHandler var0) {
  276.       return var0.object;
  277.    }
  278.  
  279.    // $FF: synthetic method
  280.    static String access$100(DataHandler var0) {
  281.       return var0.objectMimeType;
  282.    }
  283. }
  284.