~gabriel1984sibiu/jackson-core/trunk

« back to all changes in this revision

Viewing changes to src/main/java/com/fasterxml/jackson/core/io/BaseReader.java

  • Committer: Package Import Robot
  • Author(s): Wolodja Wentland
  • Date: 2013-08-10 19:27:10 UTC
  • Revision ID: package-import@ubuntu.com-20130810192710-euj21chefjiec90i
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
package com.fasterxml.jackson.core.io;
 
3
 
 
4
import java.io.*;
 
5
 
 
6
/**
 
7
 * Simple basic class for optimized readers in this package; implements
 
8
 * "cookie-cutter" methods that are used by all actual implementations.
 
9
 */
 
10
abstract class BaseReader
 
11
    extends Reader
 
12
{
 
13
    /**
 
14
     * JSON actually limits available Unicode range in the high end
 
15
     * to the same as xml (to basically limit UTF-8 max byte sequence
 
16
     * length to 4)
 
17
     */
 
18
    final protected static int LAST_VALID_UNICODE_CHAR = 0x10FFFF;
 
19
 
 
20
    final protected static char NULL_CHAR = (char) 0;
 
21
    final protected static char NULL_BYTE = (byte) 0;
 
22
 
 
23
    final protected IOContext _context;
 
24
 
 
25
    protected InputStream _in;
 
26
 
 
27
    protected byte[] _buffer;
 
28
 
 
29
    protected int _ptr;
 
30
    protected int _length;
 
31
 
 
32
    /*
 
33
    /**********************************************************
 
34
    /* Life-cycle
 
35
    /**********************************************************
 
36
     */
 
37
 
 
38
    protected BaseReader(IOContext context,
 
39
            InputStream in, byte[] buf, int ptr, int len)
 
40
    {
 
41
        _context = context;
 
42
        _in = in;
 
43
        _buffer = buf;
 
44
        _ptr = ptr;
 
45
        _length = len;
 
46
    }
 
47
 
 
48
    /*
 
49
    /**********************************************************
 
50
    /* Reader API
 
51
    /**********************************************************
 
52
     */
 
53
 
 
54
    @Override
 
55
    public void close() throws IOException
 
56
    {
 
57
        InputStream in = _in;
 
58
 
 
59
        if (in != null) {
 
60
            _in = null;
 
61
            freeBuffers();
 
62
            in.close();
 
63
        }
 
64
    }
 
65
 
 
66
    protected char[] _tmpBuf = null;
 
67
 
 
68
    /**
 
69
     * Although this method is implemented by the base class, AND it should
 
70
     * never be called by main code, let's still implement it bit more
 
71
     * efficiently just in case
 
72
     */
 
73
    @Override
 
74
    public int read() throws IOException
 
75
    {
 
76
        if (_tmpBuf == null) {
 
77
            _tmpBuf = new char[1];
 
78
        }
 
79
        if (read(_tmpBuf, 0, 1) < 1) {
 
80
            return -1;
 
81
        }
 
82
        return _tmpBuf[0];
 
83
    }
 
84
 
 
85
    /*
 
86
    /**********************************************************
 
87
    /* Internal/package methods:
 
88
    /**********************************************************
 
89
     */
 
90
 
 
91
    /**
 
92
     * This method should be called along with (or instead of) normal
 
93
     * close. After calling this method, no further reads should be tried.
 
94
     * Method will try to recycle read buffers (if any).
 
95
     */
 
96
    public final void freeBuffers()
 
97
    {
 
98
        byte[] buf = _buffer;
 
99
        if (buf != null) {
 
100
            _buffer = null;
 
101
            _context.releaseReadIOBuffer(buf);
 
102
        }
 
103
    }
 
104
 
 
105
    protected void reportBounds(char[] cbuf, int start, int len)
 
106
        throws IOException
 
107
    {
 
108
        throw new ArrayIndexOutOfBoundsException("read(buf,"+start+","+len+"), cbuf["+cbuf.length+"]");
 
109
    }
 
110
 
 
111
    protected void reportStrangeStream()
 
112
        throws IOException
 
113
    {
 
114
        throw new IOException("Strange I/O stream, returned 0 bytes on read");
 
115
    }
 
116
}