1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.store; |
7 | |
8 | import java.io.BufferedInputStream; |
9 | import java.io.IOException; |
10 | import java.io.InputStream; |
11 | import java.io.Reader; |
12 | import org.h2.value.Value; |
13 | import org.h2.value.ValueLobDb; |
14 | |
15 | /** |
16 | * This factory creates in-memory objects and temporary files. It is used on the |
17 | * client side. |
18 | */ |
19 | public class LobStorageFrontend implements LobStorageInterface { |
20 | |
21 | /** |
22 | * The table id for session variables (LOBs not assigned to a table). |
23 | */ |
24 | public static final int TABLE_ID_SESSION_VARIABLE = -1; |
25 | |
26 | /** |
27 | * The table id for temporary objects (not assigned to any object). |
28 | */ |
29 | public static final int TABLE_TEMP = -2; |
30 | |
31 | /** |
32 | * The table id for result sets. |
33 | */ |
34 | public static final int TABLE_RESULT = -3; |
35 | |
36 | private final DataHandler handler; |
37 | |
38 | public LobStorageFrontend(DataHandler handler) { |
39 | this.handler = handler; |
40 | } |
41 | |
42 | @Override |
43 | public void removeLob(ValueLobDb lob) { |
44 | // not stored in the database |
45 | } |
46 | |
47 | /** |
48 | * Get the input stream for the given lob. |
49 | * |
50 | * @param lob the lob |
51 | * @param hmac the message authentication code (for remote input streams) |
52 | * @param byteCount the number of bytes to read, or -1 if not known |
53 | * @return the stream |
54 | */ |
55 | @Override |
56 | public InputStream getInputStream(ValueLobDb lob, byte[] hmac, |
57 | long byteCount) throws IOException { |
58 | if (byteCount < 0) { |
59 | byteCount = Long.MAX_VALUE; |
60 | } |
61 | return new BufferedInputStream(new LobStorageRemoteInputStream( |
62 | handler, lob, hmac, byteCount)); |
63 | } |
64 | |
65 | @Override |
66 | public boolean isReadOnly() { |
67 | return false; |
68 | } |
69 | |
70 | @Override |
71 | public ValueLobDb copyLob(ValueLobDb old, int tableId, long length) { |
72 | throw new UnsupportedOperationException(); |
73 | } |
74 | |
75 | @Override |
76 | public void setTable(ValueLobDb lob, int tableIdSessionVariable) { |
77 | throw new UnsupportedOperationException(); |
78 | } |
79 | |
80 | @Override |
81 | public void removeAllForTable(int tableId) { |
82 | throw new UnsupportedOperationException(); |
83 | } |
84 | |
85 | @Override |
86 | public Value createBlob(InputStream in, long maxLength) { |
87 | // need to use a temp file, because the input stream could come from |
88 | // the same database, which would create a weird situation (trying |
89 | // to read a block while writing something) |
90 | return ValueLobDb.createTempBlob(in, maxLength, handler); |
91 | } |
92 | |
93 | /** |
94 | * Create a CLOB object. |
95 | * |
96 | * @param reader the reader |
97 | * @param maxLength the maximum length (-1 if not known) |
98 | * @return the LOB |
99 | */ |
100 | @Override |
101 | public Value createClob(Reader reader, long maxLength) { |
102 | // need to use a temp file, because the input stream could come from |
103 | // the same database, which would create a weird situation (trying |
104 | // to read a block while writing something) |
105 | return ValueLobDb.createTempClob(reader, maxLength, handler); |
106 | } |
107 | |
108 | @Override |
109 | public void init() { |
110 | // nothing to do |
111 | } |
112 | |
113 | } |