1 |
/* |
/* |
2 |
* $Id$ |
* ByteArrayResponseBodyReader.java |
3 |
* Copyright (C) 2004 The Free Software Foundation |
* Copyright (C) 2004 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
44 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
45 |
*/ |
*/ |
46 |
public class ByteArrayResponseBodyReader |
public class ByteArrayResponseBodyReader |
47 |
implements ResponseBodyReader |
implements ResponseBodyReader |
48 |
{ |
{ |
49 |
|
|
50 |
/** |
/** |
65 |
/** |
/** |
66 |
* Constructs a new byte array response body reader. |
* Constructs a new byte array response body reader. |
67 |
*/ |
*/ |
68 |
public ByteArrayResponseBodyReader () |
public ByteArrayResponseBodyReader() |
69 |
{ |
{ |
70 |
this (4096); |
this(4096); |
71 |
} |
} |
72 |
|
|
73 |
/** |
/** |
75 |
* initial buffer size. |
* initial buffer size. |
76 |
* @param size the initial buffer size |
* @param size the initial buffer size |
77 |
*/ |
*/ |
78 |
public ByteArrayResponseBodyReader (int size) |
public ByteArrayResponseBodyReader(int size) |
79 |
{ |
{ |
80 |
content = new byte[size]; |
content = new byte[size]; |
81 |
pos = len = 0; |
pos = len = 0; |
84 |
/** |
/** |
85 |
* This reader accepts all responses. |
* This reader accepts all responses. |
86 |
*/ |
*/ |
87 |
public boolean accept (Request request, Response response) |
public boolean accept(Request request, Response response) |
88 |
{ |
{ |
89 |
return true; |
return true; |
90 |
} |
} |
91 |
|
|
92 |
public void read (byte[] buffer, int offset, int length) |
public void read(byte[] buffer, int offset, int length) |
93 |
{ |
{ |
94 |
int l = length - offset; |
int l = length - offset; |
95 |
if (pos + l > content.length) |
if (pos + l > content.length) |
96 |
{ |
{ |
97 |
byte[] tmp = new byte[content.length * 2]; |
byte[] tmp = new byte[content.length * 2]; |
98 |
System.arraycopy (content, 0, tmp, 0, pos); |
System.arraycopy(content, 0, tmp, 0, pos); |
99 |
content = tmp; |
content = tmp; |
100 |
} |
} |
101 |
System.arraycopy (buffer, offset, content, pos, l); |
System.arraycopy(buffer, offset, content, pos, l); |
102 |
pos += l; |
pos += l; |
103 |
len = pos; |
len = pos; |
104 |
} |
} |
105 |
|
|
106 |
public void close () |
public void close() |
107 |
{ |
{ |
108 |
pos = 0; |
pos = 0; |
109 |
} |
} |
112 |
* Retrieves the content of this reader as a byte array. |
* Retrieves the content of this reader as a byte array. |
113 |
* The size of the returned array is the number of bytes read. |
* The size of the returned array is the number of bytes read. |
114 |
*/ |
*/ |
115 |
public byte[] toByteArray () |
public byte[] toByteArray() |
116 |
{ |
{ |
117 |
byte[] ret = new byte[len]; |
byte[] ret = new byte[len]; |
118 |
System.arraycopy (content, 0, ret, 0, len); |
System.arraycopy(content, 0, ret, 0, len); |
119 |
return ret; |
return ret; |
120 |
} |
} |
121 |
|
|
122 |
} |
} |
123 |
|
|