Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/thrift
diff --git a/.gitignore b/.gitignore
old mode 100644
new mode 100755
index 10c767f..9aa31aa
--- a/.gitignore
+++ b/.gitignore
@@ -30,6 +30,7 @@
 /compiler/cpp/libparse.a
 /compiler/cpp/Makefile
 /compiler/cpp/Makefile.in
+/compiler/cpp/src/windows/version.h
 /compiler/cpp/thrift
 /compiler/cpp/thriftl.cc
 /compiler/cpp/thrifty.cc
@@ -191,6 +192,7 @@
 /lib/php/test/Makefile
 /lib/php/test/Makefile.in
 /lib/php/test/phpunit.xml
+/lib/php/test/packages/
 /lib/erl/logs/
 /lib/erl/Makefile
 /lib/erl/Makefile.in
@@ -226,4 +228,20 @@
 /test/py.twisted/test_suite.pyc
 /test/rb/Makefile
 /test/rb/Makefile.in
+/tutorial/Makefile
+/tutorial/Makefile.in
+/tutorial/cpp/Makefile
+/tutorial/cpp/Makefile.in
+/tutorial/cpp/TutorialClient
+/tutorial/cpp/TutorialServer
+/tutorial/java/Makefile
+/tutorial/java/Makefile.in
+/tutorial/java/build/
+/tutorial/js/Makefile
+/tutorial/js/Makefile.in
+/tutorial/js/build/
+/tutorial/py.twisted/Makefile
+/tutorial/py.twisted/Makefile.in
+/tutorial/py/Makefile
+/tutorial/py/Makefile.in
 /ylwrap
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index d5c0bfe..aa7f843 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -62,6 +62,9 @@
     iter = parsed_options.find("android_legacy");
     android_legacy_ = (iter != parsed_options.end());
 
+    iter = parsed_options.find("sorted_containers");
+    sorted_containers_ = (iter != parsed_options.end());
+
     iter = parsed_options.find("java5");
     java5_ = (iter != parsed_options.end());
     if (java5_) {
@@ -292,6 +295,7 @@
   bool gen_hash_code_;
   bool android_legacy_;
   bool java5_;
+  bool sorted_containers_;
 };
 
 
@@ -341,9 +345,15 @@
  */
 string t_java_generator::java_type_imports() {
   string hash_builder;
+  string tree_set_and_map;
   if (gen_hash_code_) {
     hash_builder = "import org.apache.commons.lang.builder.HashCodeBuilder;\n";
   }
+  if (sorted_containers_) {
+    tree_set_and_map = string() + 
+      "import java.util.TreeSet;\n" +
+      "import java.util.TreeMap;\n";
+  }
 
   return
     string() +
@@ -364,6 +374,7 @@
     "import java.util.Set;\n" +
     "import java.util.HashSet;\n" +
     "import java.util.EnumSet;\n" +
+    tree_set_and_map + 
     "import java.util.Collections;\n" +
     "import java.util.BitSet;\n" +
     "import java.nio.ByteBuffer;\n"
@@ -2879,10 +2890,15 @@
 
   indent(out) << prefix << " = new " << type_name(ttype, false, true);
   // size the collection correctly
-  out << "("
-    << (ttype->is_list() ? "" : "2*" )
-    << obj << ".size"
-    << ");" << endl;
+  if (sorted_containers_ && (ttype->is_map() || ttype->is_set())) {
+    // TreeSet and TreeMap don't have any constructor which takes a capactity as an argument
+    out << "();" << endl;
+  } else {
+    out << "("
+      << (ttype->is_list() ? "" : "2*" )
+      << obj << ".size"
+      << ");" << endl;
+  }
 
   // For loop iterates over elements
   string i = tmp("_i");
@@ -3190,7 +3206,11 @@
   } else if (ttype->is_map()) {
     t_map* tmap = (t_map*) ttype;
     if (in_init) {
-      prefix = "HashMap";
+      if (sorted_containers_) {
+        prefix = "TreeMap";
+      } else {
+        prefix = "HashMap";
+      }
     } else {
       prefix = "Map";
     }
@@ -3200,7 +3220,11 @@
   } else if (ttype->is_set()) {
     t_set* tset = (t_set*) ttype;
     if (in_init) {
-      prefix = "HashSet";
+      if (sorted_containers_) {
+        prefix = "TreeSet";
+      } else { 
+        prefix = "HashSet";
+      }
     } else {
       prefix = "Set";
     }
@@ -4194,5 +4218,7 @@
 "    hashcode:        Generate quality hashCode methods.\n"
 "    android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and above).\n"
 "    java5:           Generate Java 1.5 compliant code (includes android_legacy flag).\n"
+"    sorted_containers:\n"
+"                     Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.\n"
 )
 
diff --git a/lib/cocoa/src/transport/TFramedTransport.h b/lib/cocoa/src/transport/TFramedTransport.h
new file mode 100644
index 0000000..fc38877
--- /dev/null
+++ b/lib/cocoa/src/transport/TFramedTransport.h
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import <Foundation/Foundation.h>
+#import "TTransport.h"
+
+@interface TFramedTransport : NSObject <TTransport> {
+    id <TTransport> mTransport;
+}
+
+- (id) initWithTransport: (id <TTransport>) transport;
+
+@end
diff --git a/lib/cocoa/src/transport/TFramedTransport.m b/lib/cocoa/src/transport/TFramedTransport.m
new file mode 100644
index 0000000..085f9b6
--- /dev/null
+++ b/lib/cocoa/src/transport/TFramedTransport.m
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TFramedTransport.h"
+#import "TTransportException.h"
+#import "TObjective-C.h"
+
+#define HEADER_SIZE 4
+#define INIT_FRAME_SIZE 1024
+
+@implementation TFramedTransport {
+    NSMutableData* writeBuffer;
+    NSMutableData* readBuffer;
+    NSUInteger readOffset;
+    uint8_t dummy_header[HEADER_SIZE];
+}
+
+- (id) initWithTransport:(id <TTransport>)transport
+{
+    mTransport = [transport retain_stub];
+    readBuffer = nil;
+    readOffset = 0;
+    writeBuffer = [[NSMutableData alloc] initWithCapacity:INIT_FRAME_SIZE];
+    [writeBuffer appendBytes:dummy_header length:HEADER_SIZE];
+    return self;
+}
+
+- (void) dealloc
+{
+    [mTransport release_stub];
+    [writeBuffer release_stub];
+    if (readBuffer != nil)
+        [readBuffer release_stub];
+    [super dealloc_stub];
+}
+
+- (void)flush
+{
+    int len = [writeBuffer length];
+    int data_len = len - HEADER_SIZE;
+    if (data_len < 0)
+        @throw [TTransportException exceptionWithReason:@"Framed transport buffer has no header"];
+
+    uint8_t i32rd[HEADER_SIZE];
+    i32rd[0] = (uint8_t)(0xff & (data_len >> 24));
+    i32rd[1] = (uint8_t)(0xff & (data_len >> 16));
+    i32rd[2] = (uint8_t)(0xff & (data_len >> 8));
+    i32rd[3] = (uint8_t)(0xff & (data_len));
+
+    // should we make a copy of the writeBuffer instead? Better for threaded operations!
+    [writeBuffer replaceBytesInRange:NSMakeRange(0, HEADER_SIZE) withBytes:i32rd length:HEADER_SIZE];
+    [mTransport write:[writeBuffer mutableBytes] offset:0 length:len];
+    [mTransport flush];
+
+    // reuse old memory buffer
+    [writeBuffer setLength:0];
+    [writeBuffer appendBytes:dummy_header length:HEADER_SIZE];
+}
+
+- (void)write:(const uint8_t *)data offset:(unsigned int)offset length:(unsigned int)length
+{
+    [writeBuffer appendBytes:data+offset length:length];
+}
+
+- (int)readAll:(uint8_t *)buf offset:(int)off length:(int)len {
+    if (readBuffer == nil) {
+        [self readFrame];
+    }
+    
+    if (readBuffer != nil) {
+        int buffer_len = [readBuffer length];
+        if (buffer_len-readOffset >= len) {
+            [readBuffer getBytes:buf range:NSMakeRange(readOffset,len)]; // copy data
+            readOffset += len;
+        } else {
+            // void the previous readBuffer data and request a new frame
+            [self readFrame];
+            [readBuffer getBytes:buf range:NSMakeRange(0,len)]; // copy data
+            readOffset = len;
+        }
+    }
+    return len;
+}
+
+- (void)readFrame
+{
+    uint8_t i32rd[HEADER_SIZE];
+    [mTransport readAll: i32rd offset: 0 length: HEADER_SIZE];
+    int size =
+        ((i32rd[0] & 0xff) << 24) |
+        ((i32rd[1] & 0xff) << 16) |
+        ((i32rd[2] & 0xff) <<  8) |
+        ((i32rd[3] & 0xff));
+
+    if (readBuffer == nil) {
+        readBuffer = [[NSMutableData alloc] initWithLength:size];
+    } else {
+        int len = [readBuffer length];
+        if (len >= size) {
+            [readBuffer setLength:size];
+        } else {
+            // increase length of data buffer
+            [readBuffer increaseLengthBy:size-len];
+        }
+    }
+    // copy into internal memory buffer
+    [mTransport readAll:[readBuffer mutableBytes] offset:0 length:size];
+}
+
+@end