Java tutorial
/* * Copyright 2013 Objectos, Fbrica de Software LTDA. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package br.com.objectos.way.io; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.List; import java.util.zip.GZIPInputStream; import br.com.objectos.comuns.io.xls.XlsFile; import br.com.objectos.core.collections.MoreCollectors; import com.google.common.base.Throwables; import com.google.common.io.Resources; import org.testng.reporters.Files; /** * @author Marcio Endo */ public class WayIOFakes { private WayIOFakes() { } public static File gunzip(Class<?> contextClass, String resourceName) { try { File file = File.createTempFile("way-io-", ".fak"); URL url = getUrl(contextClass, resourceName); InputStream input = Resources.asByteSource(url).openStream(); GZIPInputStream gzip = new GZIPInputStream(input); Files.copyFile(gzip, file); return file; } catch (IOException e) { throw Throwables.propagate(e); } } public static List<String> linesOf(byte[] worksheet) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream(worksheet); return XlsFile.parse(inputStream).getLines().stream().map(Object::toString) .collect(MoreCollectors.toImmutableList()); } public static IsXlsWritableLineBuilder isXlsWritableLineBuilder() { return new IsXlsWritableLineBuilder(); } public static File openXlsFile(Class<?> contextClass, String fileName) { return openFile(contextClass, "/xls/" + fileName); } public static InputStream open(Class<?> contextClass, String fileName) { return openStream(contextClass, "/xls/" + fileName); } private static File openFile(Class<?> contextClass, String fileName) { try { URL url = getUrl(contextClass, fileName); return new File(url.toURI()); } catch (URISyntaxException e) { throw Throwables.propagate(e); } } private static InputStream openStream(Class<?> contextClass, String fileName) { try { URL url = getUrl(contextClass, fileName); return Resources.asByteSource(url).openStream(); } catch (IOException e) { throw Throwables.propagate(e); } } private static URL getUrl(Class<?> contextClass, String fileName) { URL url = Resources.getResource(contextClass, fileName); return url; } }