summaryrefslogtreecommitdiff
path: root/lang/java/src/com/sleepycat/db/DatabaseStream.java
blob: add2740542f2dee308dd738fef5cdd1353c87391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2013, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

package com.sleepycat.db;

import com.sleepycat.db.internal.DbConstants;
import com.sleepycat.db.internal.DbStream;

/**
A database stream. The database stream is used to access the blob.
<p>
Once the database stream close method has been called, the handle may not
be accessed again.
<p>
To obtain a database stream with default attributes:
<blockquote><pre>
    DatabaseStream dbs = myCursor.openDatabaseStream(null);
</pre></blockquote>
To customize the attributes of a database stream,
use a DatabaseStreamConfig object.
<blockquote><pre>
    DatabaseStreamConfig config = new DatabaseStreamConfig();
    config.setReadOnly(true);
    DatabaseStream dbs = myCursor.openDatabaseStream(config);
</pre></blockquote>
*/
public class DatabaseStream {
    /* package */ DbStream dbs;
    /* package */ Cursor cursor;
    /* package */ DatabaseStreamConfig config;

    protected DatabaseStream(
        final Cursor cursor, final DatabaseStreamConfig config) {
        this.cursor = cursor;
        this.config = config;
    }

    DatabaseStream(final Cursor cursor,
        final DbStream dbs, final DatabaseStreamConfig config)
        throws DatabaseException {

        this.cursor = cursor;
        this.dbs = dbs;
        this.config = config;
    }

    /**
    Discard the database stream.
    <p>
    After the close method has been called, you cannot use the database stream
    handle again.
    <p>
    It is recommended to always close all database stream handles immediately
    after their use to release resources.
    <p>
    @throws DatabaseException if a failure occurs.
    */
    public synchronized void close()
        throws DatabaseException {

        if (dbs != null) {
            try {
                dbs.close(0);
            } finally {
                dbs = null;
            }
        }
    }

    /**
    Return this database stream configuration.
    <p>
    @return
    This database stream configuration.
    <p>
    @throws DatabaseException if a failure occurs.
    */
    public DatabaseStreamConfig getConfig() {
        return config;
    }

    /**
    Return the {@link com.sleepycat.db.Cursor Cursor} handle associated
    with this database stream.
    <p>
    @return
    The cursor handle associated with this database stream.
    */
    public Cursor getCursor() {
        return cursor;
    }

    /**
    Read from the blob accessed by this database stream.
    <p>
    @throws IllegalArgumentException if a failure occurs.
    <p>
    @param data the data read from the blob
    returned as output.  Its byte array does not need to be initialized by the
    caller.
    <p>
    @param offset the position in bytes in the blob where the reading starts.
    <p>
    @param size the number of bytes to read.
    <p>
    @return {@link com.sleepycat.db.OperationStatus#SUCCESS OperationStatus.SUCCESS}
    if the operation succeeds.
    <p>
    @throws IllegalArgumentException if the operation fails.
    <p>
    @throws DatabaseException if a failure occurs.
    */
    public OperationStatus read(final DatabaseEntry data, long offset, int size)
        throws DatabaseException, IllegalArgumentException {

        return OperationStatus.fromInt(
            dbs.read(data, offset, size, 0));
    }

    /**
    Return the size in bytes of the blob accessed by the database stream.
    <p>
    @return
    The size in bytes of the blob accessed by the database stream.
    <p>
    @throws DatabaseException if a failure occurs.
    */
    public long size()
        throws DatabaseException {
        return dbs.size(0);
    }

    /**
    Write to the blob accessed by the database stream.
    <p>
    @param data the data {@link com.sleepycat.db.DatabaseEntry DatabaseEntry}
    to write into the blob.
    <p>
    @param offset the position in bytes in the blob where the writing starts.
    <p>
    @return {@link com.sleepycat.db.OperationStatus#SUCCESS OperationStatus.SUCCESS}
    if the operation succeeds.
    <p>
    @throws IllegalArgumentException if the operation fails.
    <p>
    @throws DatabaseException if a failure occurs.
    */
    public OperationStatus write(final DatabaseEntry data, long offset)
        throws DatabaseException, IllegalArgumentException {

        return OperationStatus.fromInt(dbs.write(data, offset, 0));
    }

}