org.eclipse.osgi_3.7.2.v20120110-1415

ativeCodeDescriptionImpl[numNativeDesc];
    for (int i = 0; i < numNativeDesc; i++) {
      nativeDescs[i] = readNativeCodeDescription(in);
    }
    result.setPossibleSuppliers(nativeDescs);
    int supplierIndex = in.readInt();
    if (supplierIndex >= 0) {
      result.setSupplier(nativeDescs[supplierIndex]);
    }
    return result;
  }
  
  private NativeCodeDescriptionImpl readNativeCodeDescription(DataInputStream in)
    throws IOException
  {
    NativeCodeDescriptionImpl result = new NativeCodeDescriptionImpl();
    readBaseDescription(result, in);
    result.setSupplier(readBundleDescription(in));
    try
    {
      result.setFilter(readString(in, false));
    }
    catch (InvalidSyntaxException localInvalidSyntaxException) {}
    result.setLanguages(readStringArray(in));
    result.setNativePaths(readStringArray(in));
    result.setOSNames(readStringArray(in));
    result.setOSVersions(readVersionRanges(in));
    result.setProcessors(readStringArray(in));
    result.setInvalidNativePaths(in.readBoolean());
    return result;
  }
  
  private VersionRange[] readVersionRanges(DataInputStream in)
    throws IOException
  {
    int num = in.readInt();
    if (num == 0) {
      return null;
    }
    VersionRange[] result = new VersionRange[num];
    for (int i = 0; i < num; i++) {
      result[i] = readVersionRange(in);
    }
    return result;
  }
  
  private String[] readStringArray(DataInputStream in)
    throws IOException
  {
    int num = in.readInt();
    if (num == 0) {
      return null;
    }
    String[] result = new String[num];
    for (int i = 0; i < num; i++) {
      result[i] = readString(in, false);
    }
    return result;
  }
  
  private void readVersionConstraint(VersionConstraintImpl version, DataInputStream in)
    throws IOException
  {
    version.setName(readString(in, false));
    version.setVersionRange(readVersionRange(in));
  }
  
  private Version readVersion(DataInputStream in)
    throws IOException
  {
    byte tag = readTag(in);
    if (tag == 0) {
      return Version.emptyVersion;
    }
    int majorComponent = in.readInt();
    int minorComponent = in.readInt();
    int serviceComponent = in.readInt();
    String qualifierComponent = readString(in, false);
    Version result = (Version)ObjectPool.intern(new Version(majorComponent, minorComponent, serviceComponent, qualifierComponent));
    
    return result;
  }
  
  private VersionRange readVersionRange(DataInputStream in)
    throws IOException
  {
    byte tag = readTag(in);
    if (tag == 0) {
      return null;
    }
    return new VersionRange(readVersion(in), in.readBoolean(), readVersion(in), in.readBoolean());
  }
  
  public synchronized boolean loadStateDeprecated(StateImpl state, DataInputStream input, long expectedTimestamp)
    throws IOException
  {
    try
    {
      return readStateDeprecated(state, input, expectedTimestamp);
    }
    finally
    {
      input.close();
    }
  }
  
  public synchronized boolean loadState(StateImpl state, long expectedTimestamp)
    throws IOException
  {
    return readState(state, expectedTimestamp);
  }
  
  private String readString(DataInputStream in, boolean intern)
    throws IOException
  {
    byte type = in.readByte();
    if (type == 0) {
      return null;
    }
    if (intern) {
      return in.readUTF().intern();
    }
    return (String)ObjectPool.intern(in.readUTF());
  }
  
  private byte readTag(DataInputStream in)
    throws IOException
  {
    return in.readByte();
  }
  
  private DataInputStream openLazyFile()
    throws IOException
  {
    if (lazyFile == null) {
      throw new IOException();
    }
    return new DataInputStream(new BufferedInputStream(secureAction.getFileInputStream(lazyFile), 4096));
  }
  
  boolean isLazyLoaded()
  {
    return lazyLoad;
  }
  
  boolean getAccessedFlag()
  {
    return accessedFlag;
  }
  
  void setAccessedFlag(boolean accessedFlag)
  {
    this.accessedFlag = accessedFlag;
  }
  
  /* Error */
  void fullyLoad()
  {
    // Byte code:
    //   0: aload_0
    //   1: iconst_1
    //   2: invokevirtual 863	org/eclipse/osgi/internal/resolver/StateReader:setAccessedFlag	(Z)V
    //   5: aconst_null
    //   6: astore_1
    //   7: aload_0
    //   8: invokespecial 864	org/eclipse/osgi/internal/resolver/StateReader:openLazyFile	()Ljava/io/DataInputStream;
    //   11: astore_1
    //   12: iconst_0
    //   13: istore_2
    //   14: goto +13 -> 27
    //   17: aload_0
    //   18: aload_1
    //   19: iconst_0
    //   20: invokespecial 881	org/eclipse/osgi/internal/resolver/StateReader:readBundleDescriptionLazyData	(Ljava/io/DataInputStream;I)Lorg/eclipse/osgi/internal/resolver/BundleDescriptionImpl;
    //   23: pop
    //   24: iinc 2 1
    //   27: iload_2
    //   28: aload_0
    //   29: getfield 720	org/eclipse/osgi/internal/resolver/StateReader:numBundles	I
    //   32: if_icmplt -15 -> 17
    //   35: goto +32 -> 67
    //   38: astore_2
    //   39: new 368	java/lang/RuntimeException
    //   42: dup
    //   43: aload_2
    //   44: invokevirtual 743	java/io/IOException:getMessage	()Ljava/lang/String;
    //   47: aload_2
    //   48: invokespecial 754	java/lang/RuntimeException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   51: athrow
    //   52: astore_3
    //   53: aload_1
    //   54: ifnull +11 -> 65
    //   57: aload_1
    //   58: invokevirtual 734	java/io/DataInputStream:close	()V
    //   61: goto +4 -> 65
    //   64: pop
    //   65: aload_3
    //   66: athrow
    //   67: aload_1
    //   68: ifnull +11 -> 79
    //   71: aload_1
    //   72: invokevirtual 734	java/io/DataInputStream:close	()V
    //   75: goto +4 -> 79
    //   78: pop
    //   79: return
    // Line number table:
    //   Java source line #772	-> byte code offset #0
    //   Java source line #773	-> byte code offset #5
    //   Java source line #775	-> byte code offset #7
    //   Java source line #776	-> byte code offset #12
    //   Java source line #777	-> byte code offset #17
    //   Java source line #776	-> byte code offset #24
    //   Java source line #778	-> byte code offset #38
    //   Java source line #779	-> byte code offset #39
    //   Java source line #780	-> byte code offset #52
    //   Java source line #781	-> byte code offset #53
    //   Java source line #783	-> byte code offset #57
    //   Java source line #784	-> byte code offset #64
    //   Java source line #787	-> byte code offset #65
    //   Java source line #781	-> byte code offset #67
    //   Java source line #783	-> byte code offset #71
    //   Java source line #784	-> byte code offset #78
    //   Java source line #788	-> byte code offset #79
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	80	0	this	StateReader
    //   6	66	1	in	DataInputStream
    //   13	15	2	i	int
    //   38	10	2	ioe	IOException
    //   52	14	3	localObject	Object
    //   64	1	5	localIOException1	IOException
    //   78	1	6	localIOException2	IOException
    // Exception table:
    //   from	to	target	type
    //   7	35	38	java/io/IOException
    //   7	52	52	finally
    //   57	61	64	java/io/IOException
    //   71	75	78	java/io/IOException
  }
  
  /* Error */
  void fullyLoad(BundleDescriptionImpl target)
    throws IOException
  {
    // Byte code:
    //   0: aload_0
    //   1: iconst_1
    //   2: invokevirtual 863	org/eclipse/osgi/internal/resolver/StateReader:setAccessedFlag	(Z)V
    //   5: aconst_null
    //   6: astore_2
    //   7: aload_0
    //   8: invokespecial 864	org/eclipse/osgi/internal/resolver/StateReader:openLazyFile	()Ljava/io/DataInputStream;
    //   11: astore_2
    //   12: new 374	java/util/ArrayList
    //   15: dup
    //   16: invokespecial 762	java/util/ArrayList:<init>	()V
    //   19: astore_3
    //   20: aload_0
    //   21: aload_1
    //   22: aload_3
    //   23: invokespecial 878	org/eclipse/osgi/internal/resolver/StateReader:addDependencies	(Lorg/eclipse/osgi/internal/resolver/BundleDescriptionImpl;Ljava/util/List;)V
    //   26: aload_0
    //   27: aload_3
    //   28: invokespecial 868	org/eclipse/osgi/internal/resolver/StateReader:getSkipBytes	(Ljava/util/List;)[I
    //   31: astore 4
    //   33: iconst_0
    //   34: istore 5
    //   36: goto +17 -> 53
    //   39: aload_0
    //   40: aload_2
    //   41: aload 4
    //   43: iload 5
    //   45: iaload
    //   46: invokespecial 881	org/eclipse/osgi/internal/resolver/StateReader:readBundleDescriptionLazyData	(Ljava/io/DataInputStream;I)Lorg/eclipse/osgi/internal/resolver/BundleDescriptionImpl;
    //   49: pop
    //   50: iinc 5 1
    //   53: iload 5
    //   55: aload 4
    //   57: arraylength
    //   58: if_icmplt -19 -> 39
    //   61: goto +16 -> 77
    //   64: astore 6
    //   66: aload_2
    //   67: ifnull +7 -> 74
    //   70: aload_2
    //   71: invokevirtual 734	java/io/DataInputStream:close	()V
    //   74: aload 6
    //   76: athrow
    //   77: aload_2
    //   78: ifnull +7 -> 85
    //   81: aload_2
    //   82: invokevirtual 734	java/io/DataInputStream:close	()V
    //   85: return
    // Line number table:
    //   Java source line #791	-> byte code offset #0
    //   Java source line #792	-> byte code offset #5
    //   Java source line #794	-> byte code offset #7
    //   Java source line #796	-> byte code offset #12
    //   Java source line #797	-> byte code offset #20
    //   Java source line #798	-> byte code offset #26
    //   Java source line #800	-> byte code offset #33
    //   Java source line #801	-> byte code offset #39
    //   Java source line #800	-> byte code offset #50
    //   Java source line #802	-> byte code offset #64
    //   Java source line #803	-> byte code offset #66
    //   Java source line #804	-> byte code offset #70
    //   Java source line #805	-> byte code offset #74
    //   Java source line #803	-> byte code offset #77
    //   Java source line #804	-> byte code offset #81
    //   Java source line #806	-> byte code offset #85
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	86	0	this	StateReader
    //   0	86	1	target	BundleDescriptionImpl
    //   6	76	2	in	DataInputStream
    //   19	9	3	toLoad	List<BundleDescriptionImpl>
    //   31	25	4	skipBytes	int[]
    //   34	20	5	i	int
    //   64	11	6	localObject	Object
    // Exception table:
    //   from	to	target	type
    //   7	64	64	finally
  }
  
  private void addDependencies(BundleDescriptionImpl target, List<BundleDescriptionImpl> toLoad)
  {
    if ((toLoad.contains(target)) || (target.isFullyLoaded())) {
      return;
    }
    Iterator<BundleDescriptionImpl> load = toLoad.iterator();
    int i = 0;
    while (load.hasNext())
    {
      BundleDescriptionImpl bundle = (BundleDescriptionImpl)load.next();
      if (target.getLazyDataOffset() < bundle.getLazyDataOffset()) {
        break;
      }
      i++;
    }
    if (i >= toLoad.size()) {
      toLoad.add(target);
    } else {
      toLoad.add(i, target);
    }
    List<BundleDescription> deps = target.getBundleDependencies();
    for (Iterator<BundleDescription> iter = deps.iterator(); iter.hasNext();) {
      addDependencies((BundleDescriptionImpl)iter.next(), toLoad);
    }
  }
  
  private int[] getSkipBytes(List<BundleDescriptionImpl> toLoad)
  {
    int[] skipBytes = new int[toLoad.size()];
    for (int i = 0; i < skipBytes.length; i++)
    {
      BundleDescriptionImpl current = (BundleDescriptionImpl)toLoad.get(i);
      if (i == 0)
      {
        skipBytes[i] = current.getLazyDataOffset();
      }
      else
      {
        BundleDescriptionImpl previous = (BundleDescriptionImpl)toLoad.get(i - 1);
        skipBytes[i] = (current.getLazyDataOffset() - previous.getLazyDataOffset() - previous.getLazyDataSize());
      }
    }
    return skipBytes;
  }
  
  void flushLazyObjectCache()
  {
    for (Iterator<Map.Entry<Integer, Object>> entries = objectTable.entrySet().iterator(); entries.hasNext();)
    {
      Map.Entry<Integer, Object> entry = (Map.Entry)entries.next();
      Object value = entry.getValue();
      if (((value instanceof ExportPackageDescription)) || ((value instanceof GenericDescription)) || ((value instanceof ImportPackageSpecification)) || ((value instanceof BundleSpecification)) || ((value instanceof GenericSpecification))) {
        entries.remove();
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.resolver.StateReader
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.resolver;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osgi.service.resolver.BaseDescription;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.DisabledInfo;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.GenericDescription;
import org.eclipse.osgi.service.resolver.GenericSpecification;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.service.resolver.NativeCodeDescription;
import org.eclipse.osgi.service.resolver.NativeCodeSpecification;
import org.eclipse.osgi.service.resolver.StateHelper;
import org.eclipse.osgi.service.resolver.StateWire;
import org.eclipse.osgi.service.resolver.VersionConstraint;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.osgi.framework.Filter;
import org.osgi.framework.Version;

class StateWriter
{
  private final Map<Object, Integer> objectTable = new HashMap();
  private final List<Object> forcedWrite = new ArrayList();
  
  private int addToObjectTable(Object object)
  {
    Integer cur = (Integer)objectTable.get(object);
    if (cur != null) {
      return cur.intValue();
    }
    objectTable.put(object, new Integer(objectTable.size()));
    
    return objectTable.size() - 1;
  }
  
  private int getFromObjectTable(Object object)
  {
    if (objectTable != null)
    {
      Object objectResult = objectTable.get(object);
      if (objectResult != null) {
        return ((Integer)objectResult).intValue();
      }
    }
    return -1;
  }
  
  private boolean writePrefix(Object object, DataOutputStream out)
    throws IOException
  {
    if (writeIndex(object, out)) {
      return true;
    }
    int index = addToObjectTable(object);
    out.writeByte(1);
    out.writeInt(index);
    return false;
  }
  
  private void writeStateDeprecated(StateImpl state, DataOutputStream out)
    throws IOException
  {
    out.write(36);
    if (writePrefix(state, out)) {
      return;
    }
    out.writeLong(state.getTimeStamp());
    
    String[] platformPropKeys = state.getPlatformPropertyKeys();
    writePlatformProp(platformPropKeys, out);
    Dictionary[] propSet = state.getPlatformProperties();
    out.writeInt(propSet.length);
    for (int i = 0; i < propSet.length; i++)
    {
      Dictionary<Object, Object> props = propSet[i];
      out.writeInt(platformPropKeys.length);
      for (int j = 0; j < platformPropKeys.length; j++) {
        writePlatformProp(props.get(platformPropKeys[j]), out);
      }
    }
    BundleDescription[] bundles = state.getBundles();
    StateHelperImpl.getInstance().sortBundles(bundles);
    out.writeInt(bundles.length);
    if (bundles.length == 0) {
      return;
    }
    for (int i = 0; i < bundles.length; i++) {
      writeBundleDescription(bundles[i], out, false);
    }
    out.writeBoolean(state.isResolved());
    
    out.writeInt(out.size());
    for (int i = 0; i < bundles.length; i++) {
      writeBundleDescriptionLazyData(bundles[i], out);
    }
  }
  
  public void saveState(StateImpl state, File stateFile, File lazyFile)
    throws IOException
  {
    DataOutputStream outLazy = null;
    DataOutputStream outState = null;
    FileOutputStream fosLazy = null;
    FileOutputStream fosState = null;
    synchronized (monitor)
    {
      try
      {
        BundleDescription[] bundles = state.getBundles();
        StateHelperImpl.getInstance().sortBundles(bundles);
        for (int i = 0; i < bundles.length; i++)
        {
          addToObjectTable(bundles[i]);
          if (bundles[i].getHost() != null) {
            addToObjectTable(bundles[i].getHost());
          }
        }
        fosLazy = new FileOutputStream(lazyFile);
        outLazy = new DataOutputStream(new BufferedOutputStream(fosLazy));
        for (int i = 0; i < bundles.length; i++) {
          writeBundleDescriptionLazyData(bundles[i], outLazy);
        }
        fosState = new FileOutputStream(stateFile);
        outState = new DataOutputStream(new BufferedOutputStream(fosState));
        outState.write(36);
        if (writePrefix(state, outState))
        {
          if (outLazy != null)
          {
            try
            {
              outLazy.flush();
              fosLazy.getFD().sync();
            }
            catch (IOException localIOException1) {}
            try
            {
              outLazy.close();
            }
            catch (IOException localIOException2) {}
          }
          if (outState != null)
          {
            try
            {
              outState.flush();
              fosState.getFD().sync();
            }
            catch (IOException localIOException3) {}
            try
            {
              outState.close();
            }
            catch (IOException localIOException4) {}
          }
          return;
        }
        BundleDescription[] bundles;
        outState.writeLong(state.getTimeStamp());
        
        String[] platformPropKeys = state.getPlatformPropertyKeys();
        writePlatformProp(platformPropKeys, outState);
        
        Dictionary[] propSet = state.getPlatformProperties();
        outState.writeInt(propSet.length);
        for (int i = 0; i < propSet.length; i++)
        {
          Dictionary<Object, Object> props = propSet[i];
          outState.writeInt(platformPropKeys.length);
          for (int j = 0; j < platformPropKeys.length; j++) {
            writePlatformProp(props.get(platformPropKeys[j]), outState);
          }
        }
        outState.writeInt(bundles.length);
        for (int i = 0; i < bundles.length; i++) {
          writeBundleDescription(bundles[i], outState, true);
        }
        DisabledInfo[] infos = state.getDisabledInfos();
        outState.writeInt(infos.length);
        for (int i = 0; i < infos.length; i++) {
          writeDisabledInfo(infos[i], outState);
        }
        outState.writeBoolean(state.isResolved());
      }
      finally
      {
        if (outLazy != null)
        {
          try
          {
            outLazy.flush();
            fosLazy.getFD().sync();
          }
          catch (IOException localIOException5) {}
          try
          {
            outLazy.close();
          }
          catch (IOException localIOException6) {}
        }
        if (outState != null)
        {
          try
          {
            outState.flush();
            fosState.getFD().sync();
          }
          catch (IOException localIOException7) {}
          try
          {
            outState.close();
          }
          catch (IOException localIOException8) {}
        }
      }
      if (outLazy != null)
      {
        try
        {
          outLazy.flush();
          fosLazy.getFD().sync();
        }
        catch (IOException localIOException9) {}
        try
        {
          outLazy.close();
        }
        catch (IOException localIOException10) {}
      }
      if (outState != null)
      {
        try
        {
          outState.flush();
          fosState.getFD().sync();
        }
        catch (IOException localIOException11) {}
        try
        {
          outState.close();
        }
        catch (IOException localIOException12) {}
      }
    }
  }
  
  private void writePlatformProp(Object obj, DataOutputStream out)
    throws IOException
  {
    if ((!(obj instanceof String)) && (!(obj instanceof String[])))
    {
      out.writeByte(0);
    }
    else
    {
      out.writeByte(1);
      if ((obj instanceof String))
      {
        out.writeInt(1);
        writeStringOrNull((String)obj, out);
      }
      else
      {
        String[] props = (String[])obj;
        out.writeInt(props.length);
        for (int i = 0; i < props.length; i++) {
          writeStringOrNull(props[i], out);
        }
      }
    }
  }
  
  private void writeBundleDescription(BundleDescription bundle, DataOutputStream out, boolean force)
    throws IOException
  {
    if ((force) && (!forcedWrite.contains(bundle)))
    {
      int index = addToObjectTable(bundle);
      out.writeByte(1);
      out.writeInt(index);
      forcedWrite.add(bundle);
    }
    else if (writePrefix(bundle, out))
    {
      return;
    }
    out.writeLong(bundle.getBundleId());
    writeBaseDescription(bundle, out);
    out.writeInt(((BundleDescriptionImpl)bundle).getLazyDataOffset());
    out.writeInt(((BundleDescriptionImpl)bundle).getLazyDataSize());
    out.writeBoolean(bundle.isResolved());
    out.writeBoolean(bundle.isSingleton());
    out.writeBoolean(bundle.hasDynamicImports());
    out.writeBoolean(bundle.attachFragments());
    out.writeBoolean(bundle.dynamicFragments());
    writeList(out, (String[])((BundleDescriptionImpl)bundle).getDirective("mandatory"));
    writeMap(out, bundle.getAttributes());
    writeHostSpec((HostSpecificationImpl)bundle.getHost(), out, force);
    
    List<BundleDescription> dependencies = ((BundleDescriptionImpl)bundle).getBundleDependencies();
    out.writeInt(dependencies.size());
    for (Iterator<BundleDescription> iter = dependencies.iterator(); iter.hasNext();) {
      writeBundleDescription((BundleDescription)iter.next(), out, force);
    }
  }
  
  private void writeBundleDescriptionLazyData(BundleDescription bundle, DataOutputStream out)
    throws IOException
  {
    int dataStart = out.size();
    int index = getFromObjectTable(bundle);
    ((BundleDescriptionImpl)bundle).setLazyDataOffset(out.size());
    out.writeInt(index);
    
    writeStringOrNull(bundle.getLocation(), out);
    writeStringOrNull(bundle.getPlatformFilter(), out);
    
    ExportPackageDescription[] exports = bundle.getExportPackages();
    out.writeInt(exports.length);
    for (int i = 0; i < exports.length; i++) {
      writeExportPackageDesc((ExportPackageDescriptionImpl)exports[i], out);
    }
    ImportPackageSpecification[] imports = bundle.getImportPackages();
    out.writeInt(imports.length);
    for (int i = 0; i < imports.length; i++) {
      writeImportPackageSpec(imports[i], out);
    }
    BundleSpecification[] requiredBundles = bundle.getRequiredBundles();
    out.writeInt(requiredBundles.length);
    for (int i = 0; i < requiredBundles.length; i++) {
      writeBundleSpec((BundleSpecificationImpl)requiredBundles[i], out);
    }
    ExportPackageDescription[] selectedExports = bundle.getSelectedExports();
    if (selectedExports == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(selectedExports.length);
      for (int i = 0; i < selectedExports.length; i++) {
        writeExportPackageDesc((ExportPackageDescriptionImpl)selectedExports[i], out);
      }
    }
    ExportPackageDescription[] substitutedExports = bundle.getSubstitutedExports();
    if (substitutedExports == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(substitutedExports.length);
      for (int i = 0; i < substitutedExports.length; i++) {
        writeExportPackageDesc((ExportPackageDescriptionImpl)substitutedExports[i], out);
      }
    }
    ExportPackageDescription[] resolvedImports = bundle.getResolvedImports();
    if (resolvedImports == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(resolvedImports.length);
      for (int i = 0; i < resolvedImports.length; i++) {
        writeExportPackageDesc((ExportPackageDescriptionImpl)resolvedImports[i], out);
      }
    }
    BundleDescription[] resolvedRequires = bundle.getResolvedRequires();
    if (resolvedRequires == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(resolvedRequires.length);
      for (int i = 0; i < resolvedRequires.length; i++) {
        writeBundleDescription(resolvedRequires[i], out, false);
      }
    }
    String[] ees = bundle.getExecutionEnvironments();
    out.writeInt(ees.length);
    for (int i = 0; i < ees.length; i++) {
      writeStringOrNull(ees[i], out);
    }
    Map<String, Long> dynamicStamps = ((BundleDescriptionImpl)bundle).getDynamicStamps();
    if (dynamicStamps == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(dynamicStamps.size());
      for (Iterator<String> pkgs = dynamicStamps.keySet().iterator(); pkgs.hasNext();)
      {
        String pkg = (String)pkgs.next();
        writeStringOrNull(pkg, out);
        out.writeLong(((Long)dynamicStamps.get(pkg)).longValue());
      }
    }
    GenericDescription[] genericCapabilities = bundle.getGenericCapabilities();
    if (genericCapabilities == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(genericCapabilities.length);
      for (int i = 0; i < genericCapabilities.length; i++) {
        writeGenericDescription(genericCapabilities[i], out);
      }
    }
    GenericSpecification[] genericRequires = bundle.getGenericRequires();
    if (genericRequires == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(genericRequires.length);
      for (int i = 0; i < genericRequires.length; i++) {
        writeGenericSpecification(genericRequires[i], out);
      }
    }
    GenericDescription[] selectedCapabilities = bundle.getSelectedGenericCapabilities();
    if (selectedCapabilities == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(selectedCapabilities.length);
      for (int i = 0; i < selectedCapabilities.length; i++) {
        writeGenericDescription(selectedCapabilities[i], out);
      }
    }
    GenericDescription[] resolvedCapabilities = bundle.getResolvedGenericRequires();
    if (resolvedCapabilities == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(resolvedCapabilities.length);
      for (int i = 0; i < resolvedCapabilities.length; i++) {
        writeGenericDescription(resolvedCapabilities[i], out);
      }
    }
    writeNativeCode(bundle.getNativeCodeSpecification(), out);
    
    writeMap(out, ((BundleDescriptionImpl)bundle).getWiresInternal());
    
    ((BundleDescriptionImpl)bundle).setLazyDataSize(out.size() - dataStart);
  }
  
  private void writeDisabledInfo(DisabledInfo disabledInfo, DataOutputStream out)
    throws IOException
  {
    writeStringOrNull(disabledInfo.getPolicyName(), out);
    writeStringOrNull(disabledInfo.getMessage(), out);
    writeBundleDescription(disabledInfo.getBundle(), out, false);
  }
  
  private void writeBundleSpec(BundleSpecificationImpl bundle, DataOutputStream out)
    throws IOException
  {
    if (writePrefix(bundle, out)) {
      return;
    }
    writeVersionConstraint(bundle, out);
    writeBundleDescription((BundleDescription)bundle.getSupplier(), out, false);
    out.writeBoolean(bundle.isExported());
    out.writeBoolean(bundle.isOptional());
    writeMap(out, bundle.getAttributes());
  }
  
  private void writeExportPackageDesc(ExportPackageDescriptionImpl exportPackageDesc, DataOutputStream out)
    throws IOException
  {
    if (writePrefix(exportPackageDesc, out)) {
      return;
    }
    writeBaseDescription(exportPackageDesc, out);
    writeBundleDescription(exportPackageDesc.getExporter(), out, false);
    writeMap(out, exportPackageDesc.getAttributes());
    writeMap(out, exportPackageDesc.getDirectives());
    writeExportPackageDesc((ExportPackageDescriptionImpl)exportPackageDesc.getFragmentDeclaration(), out);
  }
  
  private void writeGenericDescription(GenericDescription description, DataOutputStream out)
    throws IOException
  {
    if (writePrefix(description, out)) {
      return;
    }
    writeBaseDescription(description, out);
    writeBundleDescription(description.getSupplier(), out, false);
    writeStringOrNull(description.getType() == "generic" ? null : description.getType(), out);
    Dictionary<String, Object> attrs = description.getAttributes();
    Map<String, Object> mapAttrs = new HashMap(attrs.size());
    for (Enumeration<String> keys = attrs.keys(); keys.hasMoreElements();)
    {
      String key = (String)keys.nextElement();
      mapAttrs.put(key, attrs.get(key));
    }
    writeMap(out, mapAttrs);
    Map<String, String> directives = description.getDeclaredDirectives();
    writeMap(out, directives);
    writeGenericDescription((GenericDescription)((BaseDescriptionImpl)description).getFragmentDeclaration(), out);
  }
  
  private void writeGenericSpecification(GenericSpecification specification, DataOutputStream out)
    throws IOException
  {
    if (writePrefix(specification, out)) {
      return;
    }
    writeVersionConstraint(specification, out);
    writeStringOrNull(specification.getType() == "generic" ? null : specification.getType(), out);
    GenericDescription[] suppliers = specification.getSuppliers();
    out.writeInt(suppliers == null ? 0 : suppliers.length);
    if (suppliers != null) {
      for (int i = 0; i < suppliers.length; i++) {
        writeGenericDescription(suppliers[i], out);
      }
    }
    out.writeInt(specification.getResolution());
    writeStringOrNull(specification.getMatchingFilter(), out);
  }
  
  private void writeNativeCode(NativeCodeSpecification nativeCodeSpecification, DataOutputStream out)
    throws IOException
  {
    if (nativeCodeSpecification == null)
    {
      out.writeBoolean(false);
      return;
    }
    out.writeBoolean(true);
    out.writeBoolean(nativeCodeSpecification.isOptional());
    NativeCodeDescription[] nativeDescs = nativeCodeSpecification.getPossibleSuppliers();
    int numDescs = nativeDescs == null ? 0 : nativeDescs.length;
    out.writeInt(numDescs);
    int supplierIndex = -1;
    for (int i = 0; i < numDescs; i++)
    {
      if (nativeDescs[i] == nativeCodeSpecification.getSupplier()) {
        supplierIndex = i;
      }
      writeNativeCodeDescription(nativeDescs[i], out);
    }
    out.writeInt(supplierIndex);
  }
  
  private void writeNativeCodeDescription(NativeCodeDescription nativeCodeDescription, DataOutputStream out)
    throws IOException
  {
    writeBaseDescription(nativeCodeDescription, out);
    writeBundleDescription(nativeCodeDescription.getSupplier(), out, false);
    Filter filter = nativeCodeDescription.getFilter();
    writeStringOrNull(filter == null ? null : filter.toString(), out);
    writeStringArray(nativeCodeDescription.getLanguages(), out);
    writeStringArray(nativeCodeDescription.getNativePaths(), out);
    writeStringArray(nativeCodeDescription.getOSNames(), out);
    writeVersionRanges(nativeCodeDescription.getOSVersions(), out);
    writeStringArray(nativeCodeDescription.getProcessors(), out);
    out.writeBoolean(nativeCodeDescription.hasInvalidNativePaths());
  }
  
  private void writeVersionRanges(VersionRange[] ranges, DataOutputStream out)
    throws IOException
  {
    out.writeInt(ranges == null ? 0 : ranges.length);
    if (ranges == null) {
      return;
    }
    for (int i = 0; i < ranges.length; i++) {
      writeVersionRange(ranges[i], out);
    }
  }
  
  private void writeStringArray(String[] strings, DataOutputStream out)
    throws IOException
  {
    out.writeInt(strings == null ? 0 : strings.length);
    if (strings == null) {
      return;
    }
    for (int i = 0; i < strings.length; i++) {
      writeStringOrNull(strings[i], out);
    }
  }
  
  private void writeMap(DataOutputStream out, Map<String, ?> source)
    throws IOException
  {
    if (source == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(source.size());
      Iterator<String> iter = source.keySet().iterator();
      while (iter.hasNext())
      {
        String key = (String)iter.next();
        Object value = source.get(key);
        writeStringOrNull(key, out);
        if ((value instanceof String))
        {
          out.writeByte(0);
          writeStringOrNull((String)value, out);
        }
        else if ((value instanceof String[]))
        {
          out.writeByte(1);
          writeList(out, (String[])value);
        }
        else if ((value instanceof Boolean))
        {
          out.writeByte(2);
          out.writeBoolean(((Boolean)value).booleanValue());
        }
        else if ((value instanceof Integer))
        {
          out.writeByte(3);
          out.writeInt(((Integer)value).intValue());
        }
        else if ((value instanceof Long))
        {
          out.writeByte(4);
          out.writeLong(((Long)value).longValue());
        }
        else if ((value instanceof Double))
        {
          out.writeByte(5);
          out.writeDouble(((Double)value).doubleValue());
        }
        else if ((value instanceof Version))
        {
          out.writeByte(6);
          writeVersion((Version)value, out);
        }
        else if ("java.net.URI".equals(value.getClass().getName()))
        {
          out.writeByte(7);
          writeStringOrNull(value.toString(), out);
        }
        else if ((value instanceof List))
        {
          writeList(out, (List)value);
        }
      }
    }
  }
  
  private void writeList(DataOutputStream out, List<?> list)
    throws IOException
  {
    byte type = getListType(list);
    if (type == -2) {
      return;
    }
    out.writeByte(8);
    out.writeByte(type);
    out.writeInt(list.size());
    for (Iterator localIterator = list.iterator(); localIterator.hasNext();)
    {
      Object value = localIterator.next();
      switch (type)
      {
      case 0: 
        writeStringOrNull((String)value, out);
        break;
      case 3: 
        out.writeInt(((Integer)value).intValue());
        break;
      case 4: 
        out.writeLong(((Long)value).longValue());
        break;
      case 5: 
        out.writeDouble(((Double)value).doubleValue());
        break;
      case 6: 
        writeVersion((Version)value, out);
        break;
      case 7: 
        writeStateWire((StateWire)value, out);
      }
    }
  }
  
  private void writeStateWire(StateWire wire, DataOutputStream out)
    throws IOException
  {
    VersionConstraint requirement = wire.getDeclaredRequirement();
    if ((requirement instanceof ImportPackageSpecificationImpl))
    {
      out.writeByte(0);
      writeImportPackageSpec((ImportPackageSpecificationImpl)requirement, out);
    }
    else if ((requirement instanceof BundleSpecificationImpl))
    {
      out.writeByte(1);
      writeBundleSpec((BundleSpecificationImpl)requirement, out);
    }
    else if ((requirement instanceof HostSpecificationImpl))
    {
      out.writeByte(2);
      writeHostSpec((HostSpecificationImpl)requirement, out, false);
    }
    else if ((requirement instanceof GenericSpecificationImpl))
    {
      out.writeByte(3);
      writeGenericSpecification((GenericSpecificationImpl)requirement, out);
    }
    else
    {
      throw new IllegalArgumentException("Unknown requiement type: " + requirement.getClass());
    }
    BaseDescription capability = wire.getDeclaredCapability();
    if ((capability instanceof BundleDescription)) {
      writeBundleDescription((BundleDescription)capability, out, false);
    } else if ((capability instanceof ExportPackageDescriptionImpl)) {
      writeExportPackageDesc((ExportPackageDescriptionImpl)capability, out);
    } else if ((capability instanceof GenericDescription)) {
      writeGenericDescription((GenericDescription)capability, out);
    } else {
      throw new IllegalArgumentException("Unknown capability type: " + requirement.getClass());
    }
    writeBundleDescription(wire.getRequirementHost(), out, false);
    writeBundleDescription(wire.getCapabilityHost(), out, false);
  }
  
  private byte getListType(List<?> list)
  {
    if (list.size() == 0) {
      return -1;
    }
    Object type = list.get(0);
    if ((type instanceof String)) {
      return 0;
    }
    if ((type instanceof Integer)) {
      return 3;
    }
    if ((type instanceof Long)) {
      return 4;
    }
    if ((type instanceof Double)) {
      return 5;
    }
    if ((type instanceof Version)) {
      return 6;
    }
    if ((type instanceof StateWire)) {
      return 7;
    }
    return -2;
  }
  
  private void writeList(DataOutputStream out, String[] list)
    throws IOException
  {
    if (list == null)
    {
      out.writeInt(0);
    }
    else
    {
      out.writeInt(list.length);
      for (int i = 0; i < list.length; i++) {
        writeStringOrNull(list[i], out);
      }
    }
  }
  
  private void writeBaseDescription(BaseDescription rootDesc, DataOutputStream out)
    throws IOException
  {
    writeStringOrNull(rootDesc.getName(), out);
    writeVersion(rootDesc.getVersion(), out);
  }
  
  private void writeImportPackageSpec(ImportPackageSpecification importPackageSpec, DataOutputStream out)
    throws IOException
  {
    if (writePrefix(importPackageSpec, out)) {
      return;
    }
    writeVersionConstraint(importPackageSpec, out);
    if (importPackageSpec.getBundle().isResolved()) {
      writeExportPackageDesc((ExportPackageDescriptionImpl)importPackageSpec.getSupplier(), out);
    } else {
      out.writeByte(0);
    }
    writeStringOrNull(importPackageSpec.getBundleSymbolicName(), out);
    writeVersionRange(importPackageSpec.getBundleVersionRange(), out);
    writeMap(out, importPackageSpec.getAttributes());
    writeMap(out, importPackageSpec.getDirectives());
  }
  
  private void writeHostSpec(HostSpecificationImpl host, DataOutputStream out, boolean force)
    throws IOException
  {
    if ((host != null) && (force) && (!forcedWrite.contains(host)))
    {
      int index = addToObjectTable(host);
      out.writeByte(1);
      out.writeInt(index);
      forcedWrite.add(host);
    }
    else if (writePrefix(host, out))
    {
      return;
    }
    writeVersionConstraint(host, out);
    BundleDescription[] hosts = host.getHosts();
    if (hosts == null)
    {
      out.writeInt(0);
      return;
    }
    out.writeInt(hosts.length);
    for (int i = 0; i < hosts.length; i++) {
      writeBundleDescription(hosts[i], out, force);
    }
    writeMap(out, host.getAttributes());
  }
  
  private void writeVersionConstraint(VersionConstraint constraint, DataOutputStream out)
    throws IOException
  {
    writeStringOrNull(constraint.getName(), out);
    writeVersionRange(constraint.getVersionRange(), out);
  }
  
  private void writeVersion(Version version, DataOutputStream out)
    throws IOException
  {
    if ((version == null) || (version.equals(Version.emptyVersion)))
    {
      out.writeByte(0);
      return;
    }
    out.writeByte(1);
    out.writeInt(version.getMajor());
    out.writeInt(version.getMinor());
    out.writeInt(version.getMicro());
    writeQualifier(version.getQualifier(), out);
  }
  
  private void writeVersionRange(VersionRange versionRange, DataOutputStream out)
    throws IOException
  {
    if ((versionRange == null) || (versionRange.equals(VersionRange.emptyRange)))
    {
      out.writeByte(0);
      return;
    }
    out.writeByte(1);
    writeVersion(versionRange.getMinimum(), out);
    out.writeBoolean(versionRange.getIncludeMinimum());
    writeVersion(versionRange.getMaximum(), out);
    out.writeBoolean(versionRange.getIncludeMaximum());
  }
  
  private boolean writeIndex(Object object, DataOutputStream out)
    throws IOE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd