OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [jar/] [Creator.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* Creator.java - create a new jar file
2
 Copyright (C) 2006, 2008 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath.
5
 
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
 it under the terms of the GNU General Public License as published by
8
 the Free Software Foundation; either version 2, or (at your option)
9
 any later version.
10
 
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 General Public License for more details.
15
 
16
 You should have received a copy of the GNU General Public License
17
 along with GNU Classpath; see the file COPYING.  If not, write to the
18
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 02110-1301 USA.
20
 
21
 Linking this library statically or dynamically with other modules is
22
 making a combined work based on this library.  Thus, the terms and
23
 conditions of the GNU General Public License cover the whole
24
 combination.
25
 
26
 As a special exception, the copyright holders of this library give you
27
 permission to link this library with independent modules to produce an
28
 executable, regardless of the license terms of these independent
29
 modules, and to copy and distribute the resulting executable under
30
 terms of your choice, provided that you also meet, for each linked
31
 independent module, the terms and conditions of the license of that
32
 module.  An independent module is a module which is not derived from
33
 or based on this library.  If you modify this library, you may extend
34
 this exception to your version of the library, but you are not
35
 obligated to do so.  If you do not wish to do so, delete this
36
 exception statement from your version. */
37
 
38
 
39
package gnu.classpath.tools.jar;
40
 
41
import java.io.BufferedOutputStream;
42
import java.io.ByteArrayOutputStream;
43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.FileOutputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48
import java.io.OutputStream;
49
import java.text.MessageFormat;
50
import java.util.ArrayList;
51
import java.util.HashSet;
52
import java.util.jar.Attributes;
53
import java.util.jar.JarFile;
54
import java.util.jar.JarOutputStream;
55
import java.util.jar.Manifest;
56
import java.util.zip.CRC32;
57
import java.util.zip.ZipEntry;
58
 
59
public class Creator
60
    extends Action
61
{
62
  JarOutputStream outputStream;
63
  HashSet<String> writtenItems = new HashSet<String>();
64
  // The manifest to use, or null if we don't want a manifest.
65
  Manifest manifest;
66
 
67
  private long copyFile(CRC32 crc, InputStream is, OutputStream output)
68
      throws IOException
69
  {
70
    byte[] buffer = new byte[1024];
71
    long size = 0;
72
    while (true)
73
      {
74
        int len = is.read(buffer);
75
        if (len == - 1)
76
          break;
77
        size += len;
78
        output.write(buffer, 0, len);
79
        crc.update(buffer, 0, len);
80
      }
81
    output.close();
82
    return size;
83
  }
84
 
85
  protected void writeFile(boolean isDirectory, InputStream inputFile,
86
                           String filename, boolean verbose)
87
    throws IOException
88
  {
89
    if (writtenItems.contains(filename))
90
      {
91
        if (verbose)
92
          {
93
            String msg = MessageFormat.format(Messages.getString("Creator.Ignoring"), //$NON-NLS-1$
94
                                              new Object[] { filename });
95
            System.err.println(msg);
96
          }
97
        return;
98
      }
99
 
100
    ByteArrayOutputStream out = new ByteArrayOutputStream();
101
    CRC32 crc = new CRC32();
102
    long size;
103
    if (isDirectory)
104
      {
105
        size = 0;
106
      }
107
    else
108
      {
109
        size = copyFile(crc, inputFile, out);
110
      }
111
 
112
    ZipEntry entry = new ZipEntry(filename);
113
    entry.setCrc(crc.getValue());
114
    entry.setSize(size);
115
 
116
    outputStream.putNextEntry(entry);
117
    out.writeTo(outputStream);
118
    outputStream.closeEntry();
119
    writtenItems.add(filename);
120
 
121
    if (verbose)
122
      {
123
        long csize = entry.getCompressedSize();
124
        long perc;
125
        if (size == 0)
126
          perc = 0;
127
        else
128
          perc = 100 - (100 * csize) / size;
129
        String msg = MessageFormat.format(Messages.getString("Creator.Adding"), //$NON-NLS-1$
130
                                          new Object[]
131
                                            {
132
                                              filename,
133
                                              Long.valueOf(size),
134
                                              Long.valueOf(entry.getSize()),
135
                                              Long.valueOf(perc)
136
                                            });
137
        System.err.println(msg);
138
      }
139
  }
140
 
141
  protected void writeFile(File file, String filename, boolean verbose)
142
      throws IOException
143
  {
144
    boolean isDirectory = file.isDirectory();
145
    if (isDirectory)
146
      {
147
        if (filename.charAt(filename.length() - 1) != '/')
148
          filename += '/';
149
        writeFile(isDirectory, null, filename, verbose);
150
      }
151
    else
152
      {
153
        InputStream inputStream = new FileInputStream(file);
154
        writeFile(isDirectory, inputStream, filename, verbose);
155
        inputStream.close();
156
      }
157
  }
158
 
159
  private void addEntries(ArrayList<Entry> result, Entry entry)
160
  {
161
    if (entry.file.isDirectory())
162
      {
163
        String name = entry.name;
164
        if (name.charAt(name.length() - 1) != '/')
165
          {
166
            name += '/';
167
            entry = new Entry(entry.file, name);
168
          }
169
        result.add(entry);
170
        String[] files = entry.file.list();
171
        for (int i = 0; i < files.length; ++i)
172
          addEntries(result, new Entry(new File(entry.file, files[i]),
173
                                       entry.name + files[i]));
174
      }
175
    else
176
      result.add(entry);
177
  }
178
 
179
  private ArrayList<Entry> getAllEntries(Main parameters)
180
  {
181
    ArrayList<Entry> allEntries = new ArrayList<Entry>();
182
    for (Entry entry : parameters.entries)
183
      addEntries(allEntries, entry);
184
    return allEntries;
185
  }
186
 
187
  private void writeCommandLineEntries(Main parameters)
188
      throws IOException
189
  {
190
    // We've already written the manifest, make sure to mark it.
191
    writtenItems.add("META-INF/"); //$NON-NLS-1$
192
    writtenItems.add(JarFile.MANIFEST_NAME);
193
 
194
    ArrayList<Entry> allEntries = getAllEntries(parameters);
195
    for (Entry entry : allEntries)
196
      writeFile(entry.file, entry.name, parameters.verbose);
197
  }
198
 
199
  protected Manifest createManifest(Main parameters)
200
    throws IOException
201
  {
202
    if (! parameters.wantManifest)
203
      return null;
204
    if (parameters.manifestFile != null)
205
      {
206
        // User specified a manifest file.
207
        InputStream contents = new FileInputStream(parameters.manifestFile);
208
        return new Manifest(contents);
209
      }
210
    return new Manifest();
211
  }
212
 
213
  protected void writeCommandLineEntries(Main parameters, OutputStream os)
214
    throws IOException
215
  {
216
    manifest = createManifest(parameters);
217
    /* If no version is specified, provide the same manifest version default
218
     * as Sun's jar tool */
219
    if (parameters.wantManifest)
220
      {
221
          Attributes attr = manifest.getMainAttributes();
222
          if (attr.getValue(Attributes.Name.MANIFEST_VERSION) == null)
223
              attr.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
224
          attr.putValue("Created-By", System.getProperty("java.version") +
225
                  " (" + System.getProperty("java.vendor") + ")");
226
      }
227
    outputStream = new JarOutputStream(os, manifest);
228
    // FIXME: this sets the method too late for the manifest file.
229
    outputStream.setMethod(parameters.storageMode);
230
    writeCommandLineEntries(parameters);
231
  }
232
 
233
  protected void close() throws IOException
234
  {
235
    outputStream.finish();
236
    outputStream.close();
237
  }
238
 
239
  public void run(Main parameters) throws IOException
240
  {
241
    if (parameters.archiveFile == null || parameters.archiveFile.equals("-")) //$NON-NLS-1$
242
      writeCommandLineEntries(parameters, System.out);
243
    else
244
      {
245
        OutputStream os
246
          = new BufferedOutputStream(new FileOutputStream(parameters.archiveFile));
247
        writeCommandLineEntries(parameters, os);
248
      }
249
    close();
250
  }
251
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.