001 /*
002 * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003 *
004 * http://uio.imagero.com
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 * o Redistributions of source code must retain the above copyright notice,
010 * this list of conditions and the following disclaimer.
011 *
012 * o Redistributions in binary form must reproduce the above copyright notice,
013 * this list of conditions and the following disclaimer in the documentation
014 * and/or other materials provided with the distribution.
015 *
016 * o Neither the name of Andrey Kuznetsov nor the names of
017 * its contributors may be used to endorse or promote products derived
018 * from this software without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032 package com.imagero.uio.bio;
033
034 import com.imagero.uio.UIOStreamBuilder;
035 import com.imagero.uio.bio.content.Content;
036 import com.imagero.uio.bio.content.DummyContent;
037 import com.imagero.uio.bio.content.FileCachedInputStreamContent;
038 import com.imagero.uio.bio.content.MemoryCachedInputStreamContent;
039 import com.imagero.uio.bio.content.FileCachedHTTPContent;
040 import com.imagero.uio.bio.content.HTTPContent;
041
042 import java.io.DataOutput;
043 import java.io.IOException;
044 import java.io.OutputStream;
045 import java.io.InputStream;
046 import java.io.File;
047 import java.net.URL;
048
049 /**
050 * This class could be removed in the future.
051 * Use UIOStreamBuilder to create uio streams.
052 * @author Andrey Kuznetsov
053 */
054 public class BIOFactory {
055
056 public static BufferedRandomAccessIO create(int chunkSize) {
057 IOController controller = createIOController(chunkSize);
058 BufferedRandomAccessIO out = new BufferedRandomAccessIO(controller);
059 return out;
060 }
061
062 public static BufferedRandomAccessIO create(OutputStream out) {
063 return create(out, UIOStreamBuilder.DEFAULT_CHUNK_SIZE);
064 }
065
066 public static BufferedRandomAccessIO create(final OutputStream out, int chunkSize) {
067 IOController ctrl = createIOController(chunkSize);
068 BufferedRandomAccessIO bio = new BufferedRandomAccessIO(ctrl) {
069 public void close() throws IOException {
070 controller.writeTo(out);
071 super.close();
072 }
073 };
074 return bio;
075 }
076
077 public static BufferedRandomAccessIO create(DataOutput out) {
078 return create(out, UIOStreamBuilder.DEFAULT_CHUNK_SIZE);
079 }
080
081 public static BufferedRandomAccessIO create(final DataOutput out, int chunkSize) {
082 IOController ctrl = createIOController(chunkSize);
083 BufferedRandomAccessIO bio = new BufferedRandomAccessIO(ctrl) {
084 public void close() throws IOException {
085 controller.writeTo(out);
086 super.close();
087 }
088 };
089 return bio;
090 }
091
092 public static IOController createIOController(int chunkSize) {
093 Content bc = new DummyContent();
094 IOController sb = new IOController(chunkSize, bc);
095 return sb;
096 }
097
098 public static IOController createIOController(InputStream in, File tmp, int chunkSize) {
099 Content bc;
100 if (tmp != null) {
101 try {
102 bc = new FileCachedInputStreamContent(in, tmp);
103 } catch (IOException ex) {
104 System.err.println("Unable to use file cache, switching to memory cache.");
105 ex.printStackTrace();
106 bc = new MemoryCachedInputStreamContent(in, chunkSize);
107 }
108 } else {
109 bc = new MemoryCachedInputStreamContent(in, chunkSize);
110 }
111 IOController sb = new IOController(chunkSize, bc);
112 return sb;
113 }
114
115 public static IOController createIOController(URL url) {
116 return createIOController(url, UIOStreamBuilder.DEFAULT_CHUNK_SIZE);
117 }
118
119 public static IOController createIOController(URL url, int chunkSize) {
120 return createIOController(url, null, chunkSize);
121 }
122
123 public static IOController createIOController(URL url, File tmp) {
124 return createIOController(url, tmp, UIOStreamBuilder.DEFAULT_CHUNK_SIZE);
125 }
126
127 public static IOController createIOController(URL url, File tmp, int chunkSize) {
128 Content bc;
129 if (tmp != null) {
130 try {
131 bc = new FileCachedHTTPContent(url, tmp);
132 } catch (IOException ex) {
133 ex.printStackTrace();
134 System.err.println("Unable to use file cache, switching to memory cache.");
135 bc = new HTTPContent(url);
136 }
137 } else {
138 bc = new HTTPContent(url);
139 }
140 IOController sb = new IOController(chunkSize, bc);
141 return sb;
142 }
143 }