1 /* 2 * Copyright (C) 2012 The Libphonenumber Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.i18n.phonenumbers.buildtools; 18 19 import java.io.BufferedInputStream; 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.util.jar.Attributes; 25 import java.util.jar.JarEntry; 26 import java.util.jar.JarOutputStream; 27 import java.util.jar.Manifest; 28 29 /** 30 * Implementation of the AbstractPhonePrefixDataIOHandler required by the GeneratePhonePrefixData 31 * class used here to create the output files and add them to the resulting JAR. 32 */ 33 public class JarPhonePrefixDataIOHandler extends AbstractPhonePrefixDataIOHandler { 34 35 // Base name of the output JAR files. It also forms part of the name of the package 36 // containing the generated binary data. 37 private final String jarBase; 38 // The path to the output directory. 39 private final File outputPath; 40 // The JAR output stream used by the JarPhonePrefixDataIOHandler. 41 private final JarOutputStream jarOutputStream; 42 // The package that will be used to create the JAR entry file. 43 private final Package outputPackage; 44 JarPhonePrefixDataIOHandler(File outputPath, String outputName, Package outputPackage)45 public JarPhonePrefixDataIOHandler(File outputPath, String outputName, Package outputPackage) 46 throws IOException { 47 if (outputPath.exists()) { 48 if (!outputPath.isDirectory()) { 49 throw new IOException("Expected directory: " + outputPath.getAbsolutePath()); 50 } 51 } else { 52 if (!outputPath.mkdirs()) { 53 throw new IOException("Could not create directory " + outputPath.getAbsolutePath()); 54 } 55 } 56 this.outputPath = outputPath; 57 this.jarBase = outputName; 58 this.outputPackage = outputPackage; 59 jarOutputStream = createJar(); 60 } 61 createJar()62 private JarOutputStream createJar() throws IOException { 63 Manifest manifest = new java.util.jar.Manifest(); 64 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 65 return new JarOutputStream(new FileOutputStream(new File(outputPath, jarBase + ".jar"))); 66 } 67 68 /** 69 * Adds the provided file to the created JAR. 70 */ 71 @Override addFileToOutput(File file)72 public void addFileToOutput(File file) throws IOException { 73 JarEntry entry = 74 new JarEntry( 75 outputPackage.getName().replace('.', '/') 76 + String.format("/%s/", jarBase) 77 + file.getPath()); 78 entry.setTime(file.lastModified()); 79 jarOutputStream.putNextEntry(entry); 80 BufferedInputStream bufferedInputStream = null; 81 82 try { 83 bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); 84 byte[] buffer = new byte[4096]; 85 86 for (int read; (read = bufferedInputStream.read(buffer)) > 0; ) { 87 jarOutputStream.write(buffer, 0, read); 88 } 89 if (!file.delete()) { 90 throw new IOException("Could not delete: " + file.getAbsolutePath()); 91 } 92 } finally { 93 jarOutputStream.closeEntry(); 94 closeFile(bufferedInputStream); 95 } 96 } 97 98 @Override createFile(String path)99 public File createFile(String path) { 100 return new File(path); 101 } 102 103 @Override close()104 public void close() { 105 closeFile(jarOutputStream); 106 } 107 } 108