001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.hadoop.sequencefile; 031 032import java.io.ByteArrayOutputStream; 033import java.io.IOException; 034import java.net.URI; 035import java.util.Map; 036import java.util.zip.ZipOutputStream; 037 038import org.apache.hadoop.fs.FSDataInputStream; 039import org.apache.hadoop.fs.FSDataOutputStream; 040import org.apache.hadoop.fs.FileSystem; 041import org.apache.hadoop.fs.Path; 042import org.apache.hadoop.io.BytesWritable; 043import org.apache.hadoop.io.IOUtils; 044import org.apache.hadoop.io.SequenceFile; 045import org.apache.hadoop.io.SequenceFile.CompressionType; 046import org.apache.hadoop.io.Text; 047 048/** 049 * A concrete implementation of a {@link SequenceFileUtility} for 050 * {@link SequenceFile}s with {@link Text} keys and {@link BytesWritable} 051 * values. 052 * 053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 054 */ 055public final class TextBytesSequenceFileUtility extends SequenceFileUtility<Text, BytesWritable> { 056 057 public TextBytesSequenceFileUtility(String uriOrPath, boolean read) throws IOException { 058 super(uriOrPath, read); 059 } 060 061 public TextBytesSequenceFileUtility(String uriOrPath, CompressionType compressionType, Map<String, String> metadata) 062 throws IOException 063 { 064 super(uriOrPath, compressionType, metadata); 065 } 066 067 public TextBytesSequenceFileUtility(String uriOrPath, CompressionType compressionType) throws IOException { 068 super(uriOrPath, compressionType); 069 } 070 071 public TextBytesSequenceFileUtility(URI uri, boolean read) throws IOException { 072 super(uri, read); 073 } 074 075 public TextBytesSequenceFileUtility(URI uri, CompressionType compressionType, Map<String, String> metadata) 076 throws IOException 077 { 078 super(uri, compressionType, metadata); 079 } 080 081 public TextBytesSequenceFileUtility(URI uri, CompressionType compressionType) throws IOException { 082 super(uri, compressionType); 083 } 084 085 @Override 086 protected BytesWritable readFile(FileSystem fs, Path path) throws IOException { 087 FSDataInputStream dis = null; 088 ByteArrayOutputStream baos = null; 089 090 try { 091 dis = fs.open(path); 092 baos = new ByteArrayOutputStream(); 093 094 IOUtils.copyBytes(dis, baos, config, false); 095 096 final byte[] bytes = baos.toByteArray(); 097 return new BytesWritable(bytes); 098 } finally { 099 if (dis != null) 100 try { 101 dis.close(); 102 } catch (final IOException e) { 103 } 104 ; 105 if (baos != null) 106 try { 107 baos.close(); 108 } catch (final IOException e) { 109 } 110 ; 111 } 112 } 113 114 @Override 115 protected void writeFile(FileSystem fs, Path path, BytesWritable value) throws IOException { 116 FSDataOutputStream dos = null; 117 118 try { 119 dos = fs.create(path); 120 final byte[] bytes = new byte[value.getLength()]; 121 System.arraycopy(value.getBytes(), 0, bytes, 0, bytes.length); 122 dos.write(bytes); 123 } finally { 124 if (dos != null) 125 try { 126 dos.close(); 127 } catch (final IOException e) { 128 } 129 ; 130 } 131 } 132 133 @Override 134 protected void printFile(BytesWritable value) throws IOException { 135 System.out.write(value.getBytes()); 136 } 137 138 @Override 139 protected void writeZipData(ZipOutputStream zos, BytesWritable value) throws IOException { 140 zos.write(value.getBytes(), 0, value.getLength()); 141 } 142}