• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.core.testsupport;
2 
3 import java.io.IOException;
4 
5 import com.fasterxml.jackson.core.JsonParser;
6 import com.fasterxml.jackson.core.JsonToken;
7 import com.fasterxml.jackson.core.async.ByteArrayFeeder;
8 
9 /**
10  * Helper class used with async parser
11  */
12 public class AsyncReaderWrapperForByteArray extends AsyncReaderWrapper
13 {
14     private final byte[] _doc;
15     private final int _bytesPerFeed;
16     private final int _padding;
17 
18     private int _offset;
19     private int _end;
20 
AsyncReaderWrapperForByteArray(JsonParser sr, int bytesPerCall, byte[] doc, int padding)21     public AsyncReaderWrapperForByteArray(JsonParser sr, int bytesPerCall,
22             byte[] doc, int padding)
23     {
24         super(sr);
25         _bytesPerFeed = bytesPerCall;
26         _doc = doc;
27         _offset = 0;
28         _end = doc.length;
29         _padding = padding;
30     }
31 
32     @Override
nextToken()33     public JsonToken nextToken() throws IOException
34     {
35         JsonToken token;
36 
37         while ((token = _streamReader.nextToken()) == JsonToken.NOT_AVAILABLE) {
38             ByteArrayFeeder feeder = (ByteArrayFeeder) _streamReader.getNonBlockingInputFeeder();
39             if (!feeder.needMoreInput()) {
40                 throw new IOException("Got NOT_AVAILABLE, could not feed more input");
41             }
42             int amount = Math.min(_bytesPerFeed, _end - _offset);
43             if (amount < 1) { // end-of-input?
44                 feeder.endOfInput();
45             } else {
46                 // padding?
47                 if (_padding == 0) {
48                     feeder.feedInput(_doc, _offset, _offset+amount);
49                 } else {
50                     byte[] tmp = new byte[amount + _padding + _padding];
51                     System.arraycopy(_doc, _offset, tmp, _padding, amount);
52                     feeder.feedInput(tmp, _padding, _padding+amount);
53                 }
54                 _offset += amount;
55             }
56         }
57         return token;
58     }
59 }
60