Changeset 39526ce


Ignore:
Timestamp:
Mar 30, 2011 9:43:49 AM (8 years ago)
Author:
Ted Faber <faber@…>
Branches:
abac0-leak, abac0-mei, compt_changes, gec13, master, mei-id, mei-rt0-n, mei_rt0, mei_rt2, mei_rt2_fix_1, meiyap-rt1, meiyap1, rt2, tvf-new-xml
Children:
18d9cfa
Parents:
20d3d3c
Message:

More remove File dependencies for read/write

Location:
java
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • java/GraphTest.java

    rad24705 r39526ce  
    3838                    ctxt.load_attribute_file(f);
    3939                else if (f.getPath().endsWith(".zip"))
    40                     ctxt.load_zip_file(f, errs);
     40                    ctxt.load_zip(f, errs);
    4141                else if (f.getPath().endsWith(".rt0"))
    42                     ctxt.load_rt0_file(f, errs);
     42                    ctxt.load_rt0(f, errs);
    4343                else
    4444                    System.out.println(f + " of unknown type");
     
    8282        }
    8383        try {
    84             ctxt.write_zip_file(new File("./testout.zip"), true, true);
     84            ctxt.write_zip(new File("./testout.zip"), true, true);
    8585        }
    8686        catch (IOException ioe) {
  • java/net/deterlab/abac/Context.java

    rad24705 r39526ce  
    547547
    548548    /**
     549     * Read the current ZipEntry's bytes from z.  Tedious because there's no
     550     * way to reliably tell how big the entry is, so we have to rely on a
     551     * simple expanding array read of the bytes.
     552     */
     553    protected byte[] readCurrentZipEntry(ZipInputStream z) throws IOException {
     554        final int bsize = 4096;
     555        byte[] buf = new byte[bsize];
     556        byte[] rv = new byte[0];
     557        int r = 0;
     558
     559        // z.read returns -1 at the end of entry
     560        while ((r = z.read(buf, 0, bsize)) != -1 ) {
     561            byte[] b = new byte[rv.length + r];
     562
     563            System.arraycopy(rv, 0, b, 0, rv.length);
     564            System.arraycopy(buf, 0, b, rv.length, r);
     565            rv = b;
     566        }
     567        return rv;
     568    }
     569
     570    /**
    549571     * Import a zip file.  First import all the identities
    550572     * (pem), then the credentials (der) into the credential graph then any
     
    552574     * PEM files are put in there.  If errors is not null, errors reading files
    553575     * are added indexed by filename.  This is a jabac extension.
    554      * @param zf the File to read
     576     * @param s the InputStream to read
    555577     * @param keys a Collection into which to insert unmatched keys
    556578     * @param errors a Map from entry name to generated exception
     
    558580     *                     returned in the errors parameter.
    559581     */
    560     public void load_zip_file(File zf, Collection<KeyPair> keys,
     582    public void load_zip(InputStream s, Collection<KeyPair> keys,
    561583            Map<String, Exception> errors) throws IOException {
    562         Vector<ZipEntry> derEntries = new Vector<ZipEntry>();
     584        Map<String, byte[]> derEntries = new HashMap<String, byte[]>();
    563585        Map<String, Identity> ids = new TreeMap<String, Identity>();
    564586        Map<String, KeyPair> kps = new TreeMap<String, KeyPair>();
    565 
    566         ZipFile z = new ZipFile(zf);
    567 
    568         for (Enumeration<? extends ZipEntry> ze = z.entries();
    569                 ze.hasMoreElements();) {
    570             ZipEntry  f = ze.nextElement();
     587        int entries = 0;
     588
     589        ZipInputStream z = new ZipInputStream(s);
     590
     591        for (ZipEntry ze = z.getNextEntry(); ze != null; ze = z.getNextEntry()){
    571592            try {
     593                entries++;
     594                byte[] buf = readCurrentZipEntry(z);
    572595                PEMReader r = new PEMReader(
    573                         new InputStreamReader(z.getInputStream(f)));
     596                        new InputStreamReader(new ByteArrayInputStream(buf)));
    574597                Object o = readPEM(r);
    575598
     
    605628                else {
    606629                    // Not a PEM file
    607                     derEntries.add(f);
     630                    derEntries.put(ze.getName(),buf);
    608631                    continue;
    609632                }
    610633            }
    611634            catch (Exception e ) {
    612                 if (errors != null ) errors.put(f.getName(), e);
    613             }
    614         }
    615 
    616         for ( ZipEntry f : derEntries ) {
     635                if (errors != null ) errors.put(ze.getName(), e);
     636            }
     637        }
     638
     639        for ( String k: derEntries.keySet() ) {
    617640            try {
    618                 add_credential(new Credential(z.getInputStream(f),
     641                add_credential(new Credential(
     642                            new ByteArrayInputStream(derEntries.get(k)),
    619643                            m_identities));
    620644            }
    621645            catch (Exception e ) {
    622                 if (errors != null ) errors.put(f.getName(), e);
    623             }
    624         }
    625     }
    626     /**
    627      * Equivalent to load_zip_file(d, null, null).
     646                if (errors != null ) errors.put(k, e);
     647            }
     648        }
     649
     650        if (entries == 0)
     651            throw new IOException("Not a ZIP file (or empty ZIP file)");
     652    }
     653    /**
     654     * Equivalent to load_zip(s, null, null).
     655     * @param s the InputStream to read
     656     * @throws IOException if the file is unreadable. To see per-entry
     657     *                      exceptions use a signature with the errors parameter
     658     */
     659    public void load_zip(InputStream s)
     660            throws IOException {
     661        load_zip(s, null, null);
     662    }
     663    /**
     664     * Equivalent to load_zip(s, null, errors).
     665     * @param s the InputStream to read
     666     * @param errors a Map from entry name to generated exception
     667     * @throws IOException if the file is unreadable.  Per entry exceptions are
     668     *                     returned in the errors parameter.
     669     */
     670    public void load_zip(InputStream s,
     671            Map<String, Exception> errors) throws IOException {
     672        load_zip(s, null, errors);
     673    }
     674    /**
     675     * Equivalent to load_zip(s, keys, null).
     676     * @param s the InputStream to read
     677     * @param keys a Collection into which to insert unmatched keys
     678     * @throws IOException if the file is unreadable. To see per-entry
     679     *                      exceptions use a signature with the errors parameter
     680     */
     681    public void load_zip(InputStream s,
     682            Collection<KeyPair> keys) throws IOException {
     683        load_zip(s, keys, null);
     684    }
     685
     686    /**
     687     * Loads a zip file.  Equivalent to
     688     * load_zip(new FileInputStream(zf), keys, errors).
     689     * @param zf the File to read
     690     * @param keys a Collection into which to insert unmatched keys
     691     * @param errors a Map from entry name to generated exception
     692     * @throws IOException if the file is unreadable.  Per entry exceptions are
     693     *                     returned in the errors parameter.
     694     */
     695    public void load_zip(File zf, Collection<KeyPair> keys,
     696            Map<String, Exception> errors) throws IOException {
     697        load_zip(new FileInputStream(zf), keys, errors);
     698    }
     699    /**
     700     * Equivalent to load_zip(d, null, null).
    628701     * @param d the File to read
    629702     * @throws IOException if the file is unreadable. To see per-entry
    630703     *                      exceptions use a signature with the errors parameter
    631704     */
    632     public void load_zip_file(File d)
     705    public void load_zip(File d)
    633706            throws IOException {
    634         load_zip_file(d, null, null);
    635     }
    636     /**
    637      * Equivalent to load_zip_file(d, null, errors).
     707        load_zip(d, null, null);
     708    }
     709    /**
     710     * Equivalent to load_zip(d, null, errors).
    638711     * @param d the File to read
    639712     * @param errors a Map from entry name to generated exception
     
    641714     *                     returned in the errors parameter.
    642715     */
    643     public void load_zip_file(File d,
     716    public void load_zip(File d,
    644717            Map<String, Exception> errors) throws IOException {
    645         load_zip_file(d, null, errors);
    646     }
    647     /**
    648      * Equivalent to load_zip_file(d, keys, null).
     718        load_zip(d, null, errors);
     719    }
     720    /**
     721     * Equivalent to load_zip(d, keys, null).
    649722     * @param d the File to read
    650723     * @param keys a Collection into which to insert unmatched keys
     
    652725     *                      exceptions use a signature with the errors parameter
    653726     */
    654     public void load_zip_file(File d,
     727    public void load_zip(File d,
    655728            Collection<KeyPair> keys) throws IOException {
    656         load_zip_file(d, keys, null);
     729        load_zip(d, keys, null);
    657730    }
    658731
     
    821894     * Spaces are not significant around the arrow and the tail can be as long
    822895     * as needed.
    823      * @param f the File to load
     896     * @param s the InputStream to load
    824897     * @param errors a Map of lines to errors
    825      * @throws IOException if there is an error getting the file open
    826      */
    827     public void load_rt0_file(File f, Map<String, Exception> errors)
     898     * @throws IOException if there is an error getting the file open or in
     899     * format
     900     */
     901    public void load_rt0(InputStream s)
    828902            throws IOException {
    829903        Pattern comment = Pattern.compile("(^\\s*#|^\\s*$)");
    830         Pattern rule = Pattern.compile("([\\w\\.]+)\\s*<-+\\s*([\\w\\.\\&]+)");
    831         LineNumberReader r = new LineNumberReader(new FileReader(f));
     904        Pattern rule = Pattern.compile("([\\w\\.]+)\\s*<-+\\s*(.+)");
     905        LineNumberReader r = new LineNumberReader(new InputStreamReader(s));
    832906        String line = null;
    833907
     
    840914                add_credential(new Credential(new Role(rm.group(1)),
    841915                            new Role(rm.group(2))));
    842             else if (errors != null)
    843                 errors.put("Line " + r.getLineNumber(),
    844                         new RuntimeException("Unexpected format"));
    845         }
    846     }
    847 
    848     /**
    849      * Equivalent to load_rt0_file(d, null).
     916            else
     917                throw new RuntimeException("Unexpected format: line " +
     918                        r.getLineNumber());
     919        }
     920    }
     921    /**
     922     * Equivalent to load_rt0(new FileInputStream(f)
    850923     * @param f the File to load
    851924     * @throws IOException if there is an error getting the file open
    852925     */
    853     public void load_rt0_file(File f) throws IOException {
    854         load_rt0_file(f, null);
     926    public void load_rt0(File f) throws IOException {
     927        load_rt0(new FileInputStream(f));
    855928    }
    856929       
     
    860933     * entry for each credential or identity.  The files are all zipped in a
    861934     * directory derived from the filename.
    862      * @param f the File to write
     935     * @param s the OutputStream to write
    863936     * @param allIDs a boolean, if true write certificates for all Identities,
    864937     * whether used in signing a credential or not.
     
    867940     * @throws IOException if there is a problem writing the file.
    868941     */
    869     public void write_zip_file(File f, boolean allIDs, boolean withPrivateKeys)
    870             throws IOException {
    871         ZipOutputStream z = new ZipOutputStream(new FileOutputStream(f));
     942    public void write_zip(OutputStream s, boolean allIDs,
     943            boolean withPrivateKeys) throws IOException {
     944        ZipOutputStream z = new ZipOutputStream(s);
    872945        Set<Identity> ids = allIDs ?  m_identities : new TreeSet<Identity>();
    873         String baseDir = f.getName();
     946        String baseDir = "creds";
    874947        int idx = baseDir.indexOf('.');
    875948
     
    895968        }
    896969        z.close();
     970    }
     971    /**
     972     * Equivalent to
     973     * write_zip(new FileOutputStream(f), allIDs, withPrivateKeys).
     974     * @param f the File to write
     975     * @param allIDs a boolean, if true write certificates for all Identities,
     976     * whether used in signing a credential or not.
     977     * @param withPrivateKeys a boolean, if true write the Identities as PEM
     978     * file containing both the certificate and the private keys.
     979     * @throws IOException if there is a problem writing the file.
     980     */
     981    public void write_zip(File f, boolean allIDs, boolean withPrivateKeys)
     982            throws IOException {
     983        write_zip(new FileOutputStream(f), allIDs, withPrivateKeys);
    897984    }
    898985
Note: See TracChangeset for help on using the changeset viewer.