comparison src/main/java/org/msgpack/io/LinkedBufferOutput.java @ 0:cb825acd883a

first commit
author sugi
date Sat, 18 Oct 2014 15:06:15 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cb825acd883a
1 //
2 // MessagePack for Java
3 //
4 // Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 package org.msgpack.io;
19
20 import java.util.LinkedList;
21
22 public final class LinkedBufferOutput extends BufferedOutput {
23 private static final class Link {
24 final byte[] buffer;
25 final int offset;
26 final int size;
27
28 Link(byte[] buffer, int offset, int size) {
29 this.buffer = buffer;
30 this.offset = offset;
31 this.size = size;
32 }
33 }
34
35 private LinkedList<Link> link;
36 private int size;
37
38 public LinkedBufferOutput(int bufferSize) {
39 super(bufferSize);
40 link = new LinkedList<Link>();
41 }
42
43 public byte[] toByteArray() {
44 byte[] bytes = new byte[size + filled];
45 int off = 0;
46 for (Link l : link) {
47 System.arraycopy(l.buffer, l.offset, bytes, off, l.size);
48 off += l.size;
49 }
50 if (filled > 0) {
51 System.arraycopy(buffer, 0, bytes, off, filled);
52 }
53 return bytes;
54 }
55
56 public int getSize() {
57 return size + filled;
58 }
59
60 @Override
61 protected boolean flushBuffer(byte[] b, int off, int len) {
62 link.add(new Link(b, off, len));
63 size += len;
64 return false;
65 }
66
67 public void clear() {
68 link.clear();
69 size = 0;
70 filled = 0;
71 }
72
73 @Override
74 public void close() {
75 }
76 }