Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common / test / src / org / tizen / common / JarSource.java
1 /*
2  * Common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * BonYong Lee <bonyong.lee@samsung.com>
8  * 
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Contributors:
22  * - S-Core Co., Ltd
23  *
24  */
25 package org.tizen.common;
26
27 import static org.tizen.common.util.IOUtil.getBytes;
28 import static org.tizen.common.util.IOUtil.tryClose;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.jar.JarEntry;
35 import java.util.jar.JarFile;
36
37 /**
38  * <p>
39  * JarSource.
40  * 
41  * Read jar file and provide class {@link InputStream}
42  * </p>
43  * 
44  * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
45  */
46 public class
47 JarSource
48 implements ClassSource
49 {
50
51         /**
52          * Jar file path
53          */
54         protected final String path;
55
56         /**
57          * Constructor with jar file path
58          * 
59          * @param location jar path
60          */
61         public
62         JarSource(
63                 String location
64         )
65         {
66                 if ( null == location )
67                 {
68                         throw new NullPointerException();
69                 }
70                 this.path = location;
71         }
72
73         /* (non-Javadoc)
74          * @see org.tizen.common.ClassSource#getResource(java.lang.String)
75          */
76         @Override
77         public
78         InputStream
79         getResource(
80                 final String path
81         )
82         throws IOException
83         {
84                 final JarFile file = new JarFile( new File( this.path ) );
85                 final JarEntry entry = file.getJarEntry( path );
86                 if ( null == entry )
87                 {
88                         return null;
89                 }
90                 InputStream in = null;
91                 try
92                 {
93                         in = file.getInputStream( entry );
94                         ByteArrayInputStream byteIn = new ByteArrayInputStream( getBytes( in ) );
95                         return byteIn;
96                 }
97                 finally
98                 {
99                         tryClose( in );
100                         file.close();
101                 }
102         }
103 }