EMMA Coverage Report (generated Tue May 16 15:34:38 CDT 2006)
[all classes][com.mysql.management.util]

COVERAGE SUMMARY FOR SOURCE FILE [Streams.java]

nameclass, %method, %block, %line, %
Streams.java100% (3/3)100% (14/14)91%  (258/283)97%  (73.1/75)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Streams$1100% (1/1)100% (2/2)78%  (36/46)91%  (8.2/9)
inner (): void 100% (1/1)71%  (24/34)90%  (7.2/8)
Streams$1 (Streams, String, File): void 100% (1/1)100% (12/12)100% (1/1)
     
class Streams100% (1/1)100% (10/10)93%  (202/217)98%  (61.9/63)
expandResourceJarInner (File, String): void 100% (1/1)67%  (20/30)89%  (7.1/8)
expandEachEntry (File, JarInputStream): void 100% (1/1)90%  (43/48)98%  (13.8/14)
Streams (): void 100% (1/1)100% (8/8)100% (3/3)
copy (InputStream, OutputStream): void 100% (1/1)100% (7/7)100% (2/2)
copy (InputStream, OutputStream, boolean, boolean): void 100% (1/1)100% (41/41)100% (16/16)
createFileFromResource (String, File): void 100% (1/1)100% (18/18)100% (6/6)
expandResourceJar (File, String): void 100% (1/1)100% (10/10)100% (3/3)
getResourceAsStream (ClassLoader, String): InputStream 100% (1/1)100% (26/26)100% (5/5)
getResourceAsStream (String): InputStream 100% (1/1)100% (9/9)100% (2/2)
readString (InputStream): String 100% (1/1)100% (20/20)100% (4/4)
     
class Streams$2100% (1/1)100% (2/2)100% (20/20)100% (3/3)
Streams$2 (Streams, File, String): void 100% (1/1)100% (12/12)100% (1/1)
inner (): void 100% (1/1)100% (8/8)100% (2/2)

1/*
2 Copyright (C) 2004 MySQL AB
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as 
6 published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 
17 */
18package com.mysql.management.util;
19 
20import java.io.BufferedInputStream;
21import java.io.BufferedOutputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.File;
24import java.io.FileNotFoundException;
25import java.io.FileOutputStream;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.OutputStream;
29import java.util.MissingResourceException;
30import java.util.jar.JarEntry;
31import java.util.jar.JarInputStream;
32 
33/**
34 * Stream operation utility methods.
35 * 
36 * This class is final simply as a hint to the compiler, it may be un-finalized
37 * safely.
38 * 
39 * @author Eric Herman <eric@mysql.com>
40 * @version $Id: Streams.java,v 1.12 2005/12/01 21:45:31 eherman Exp $
41 */
42public final class Streams {
43 
44    public static final String RESOURCE_SEPARATOR = "/";
45 
46    private static final int END_OF_STREAM = -1;
47 
48    private Exceptions exceptions;
49 
50    public Streams() {
51        this.exceptions = new Exceptions();
52    }
53 
54    /**
55     * Reads the data from the Input stream and writes to the output stream
56     * Buffers each stream. Terminates when a read from the Input stream results
57     * in EOF
58     */
59    public void copy(InputStream from, OutputStream to) throws IOException {
60        copy(from, to, true, false);
61    }
62 
63    void copy(InputStream from, OutputStream to, boolean buffer,
64            boolean terminateOnFailure) throws IOException {
65        if (buffer) {
66            from = new BufferedInputStream(from);
67            to = new BufferedOutputStream(to);
68        }
69        while (true) {
70            int i;
71            try {
72                i = from.read();
73                if (i == END_OF_STREAM) {
74                    break;
75                }
76                to.write(i);
77            } catch (Exception e) {
78                if (terminateOnFailure) {
79                    break;
80                }
81                if (e instanceof IOException) {
82                    throw (IOException) e;
83                }
84                throw exceptions.toRuntime(e);
85            }
86        }
87        to.flush();
88    }
89 
90    /** reads the entire contents of stream into a String */
91    public String readString(InputStream from) throws IOException {
92        ByteArrayOutputStream buf = new ByteArrayOutputStream();
93        try {
94            copy(from, buf);
95            return buf.toString();
96        } finally {
97            buf.close();
98        }
99    }
100 
101    /**
102     * Uses ClassLoader which loaded Streams.class to obtain resource
103     * 
104     * @return an InputStream
105     * @throws MissingResourceException
106     */
107    public InputStream getResourceAsStream(String name) {
108        ClassLoader classLoader = getClass().getClassLoader();
109        return getResourceAsStream(classLoader, name);
110    }
111 
112    /**
113     * Convenience method to check for MissingResource (null stream)
114     * 
115     * @return an InputStream
116     * @throws MissingResourceException
117     */
118    public InputStream getResourceAsStream(ClassLoader classLoader, String name) {
119        InputStream is = classLoader.getResourceAsStream(name);
120        if (is == null) {
121            String msg = "Resource '" + name + "' not found";
122            throw new MissingResourceException(msg, null, name);
123        }
124        return is;
125    }
126 
127    /**
128     * Copies a resource to the location specified by the File parameter.
129     */
130    public void createFileFromResource(final String resourceName,
131            final File file) {
132        File parent = file.getParentFile();
133        if (parent != null) {
134            parent.mkdirs();
135        }
136        Exceptions.VoidBlock block = new Exceptions.VoidBlock() {
137            public void inner() throws Exception {
138                InputStream is = getResourceAsStream(resourceName);
139                try {
140                    FileOutputStream fos = new FileOutputStream(file);
141                    try {
142                        copy(is, fos);
143                    } finally {
144                        fos.close();
145                    }
146                } finally {
147                    is.close();
148                }
149            }
150        };
151        block.exec();
152 
153    }
154 
155    /**
156     * If the jar exists as a resource, the contents of the jar will be expanded
157     * on the file system in the location specified by the File parameter.
158     * 
159     * If any files already exist, they will <b>not </b> be over-written.
160     */
161    public void expandResourceJar(final File outputDir,
162            final String jarResourceName) {
163        Exceptions.VoidBlock block = new Exceptions.VoidBlock() {
164            public void inner() throws Exception {
165                expandResourceJarInner(outputDir, jarResourceName);
166            }
167        };
168        block.exec();
169    }
170 
171    private void expandResourceJarInner(File outputDir, String jarResourceName)
172            throws IOException {
173        InputStream is = getResourceAsStream(jarResourceName);
174        try {
175            JarInputStream jis = new JarInputStream(is);
176            try {
177                expandEachEntry(outputDir, jis);
178            } finally {
179                jis.close();
180            }
181        } finally {
182            is.close();
183        }
184    }
185 
186    private void expandEachEntry(File outputDir, JarInputStream jis)
187            throws IOException, FileNotFoundException {
188        while (true) {
189            JarEntry entry = jis.getNextJarEntry();
190            if (entry == null) {
191                break;
192            }
193 
194            File file = new File(outputDir, entry.getName());
195            if (!file.exists()) {
196                if (entry.isDirectory()) {
197                    file.mkdirs();
198                } else {
199                    File parent = file.getParentFile();
200                    parent.mkdirs();
201                    FileOutputStream fos = new FileOutputStream(file);
202                    try {
203                        copy(jis, fos);
204                    } finally {
205                        fos.close();
206                    }
207                }
208            }
209        }
210    }
211}

[all classes][com.mysql.management.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov