java

mathpunk 2023-03-09T21:39:04.680629Z

I’m attempting to do serious business with Java interop for pretty much the first time. I know way more Clojure than Java. Can someone explain what’s happening here? (An artifacts is an array of bytes that, had I just downloaded it, would be a zip file):

(let [{:keys [artifacts job]} (first batched-with-artifacts)]
  (type artifacts)                                ;; clojure.lang.PersistentVector
  (type (.toArray artifacts))                     ;;[Ljava.lang.Object; ...I thought it would be java.lang.Array or something
  (ByteArrayInputStream. (.toArray artifacts)))   ;; THROWS


Execution error (ClassCastException) at logicgate.pass-fail-stats.data-acquisition/eval53644 (data_acquisition.clj:244). class [Ljava.lang.Object; cannot be cast to class [B ([Ljava.lang.Object; and [B are in module java.base of loader 'bootstrap') 

2023-03-09T21:40:04.017579Z

its an array of objects

2023-03-09T21:41:12.081209Z

[Ljava.lang.Object;
is jvm for array of Objects

2023-03-09T21:41:36.495839Z

an array of primitive bytes is [B

mathpunk 2023-03-09T21:42:01.604709Z

(-> artifacts first type) => java.lang.Long

2023-03-09T21:42:19.429309Z

it doesn't matter the actualy type of the elements

mathpunk 2023-03-09T21:43:14.168529Z

does that mean that what I think is an array of bytes actually isn’t? or do I need to tell Java, you think this is an object but I know more about it, it’s specifically a byte ?

2023-03-09T21:43:14.816959Z

if you look at the other thread I gave some example code for turning a vector of numbers into a byte array

mathpunk 2023-03-09T21:43:39.704779Z

oh shit, you sure did

mathpunk 2023-03-09T21:43:58.335489Z

i went ahead and started trying to apply Alex’s starting point and forgot you did that uh…. not a coercion but….

mathpunk 2023-03-09T21:44:04.021189Z

that transformation

2023-03-09T21:44:36.250099Z

what you have is a vector of java.lang.Longs, those are boxed numbers, numbers as Objects, because the general purpose clojure vector only can hold Objects

mathpunk 2023-03-09T21:44:43.449809Z

i just kind of thought, “well a clojure.lang.PersistentVector probably knows how to become an array, and a ByteInputStream would know what to do with that…”

2023-03-09T21:45:15.936499Z

so when you ask for an array, it returns an object array, which each element is a Long

mathpunk 2023-03-09T21:48:23.734109Z

(.toArray artifacts) => “An array of, I dunno, Stuff. You didn’t say” (into-array Byte/TYPE artifacts) => “Bytes have a type; we’ll get that from the Byte namespace, and specify what this array is full of.”

2023-03-09T21:51:42.300969Z

Byte/TYPE is actually a static field reference

mathpunk 2023-03-09T21:52:10.276489Z

HMM and yet, streams don’t know how to construct from an array of bytes…. I’ve gotta be missing something on either constructors or a hierarchy

(let [bytes (into-array Byte/TYPE artifacts)]
    (BufferedInputStream. bytes) ;; error
    (InputStream. bytes))        ;; error

mathpunk 2023-03-09T21:52:55.148689Z

What I’m attempting to replace is the bit Alex Miller linked where it goes, (ZipInputStream. (BufferedInputStream. (FileInputStream. zip-file)))

2023-03-09T21:53:05.089699Z

you need to wrap the byte array with a bytearrayinputstream

mathpunk 2023-03-09T21:53:10.156209Z

I don’t have a file, so I figured, it’s binary, so that’s InputStream?

2023-03-09T21:53:52.978869Z

an inputstream has extra associate state, so not just the bytes to be read, needs to keep track of where you at in the read

mathpunk 2023-03-09T21:54:04.496859Z

haha ok. I read a comic where someone depicted Java becoming very upset because someone handed it a cup of coffee “But you didn’t say it was a small cup of coffee!!!”

😁 1
mathpunk 2023-03-09T21:54:09.482399Z

and I’m starting to get it

mathpunk 2023-03-09T21:55:25.008249Z

sweeeet

(let [bytes (into-array Byte/TYPE artifacts)]
    (ZipInputStream. (ByteArrayInputStream. bytes)))

mathpunk 2023-03-09T21:55:38.541689Z

mathpunk 2023-03-09T21:56:29.959669Z

oh excellent! I need an overview of these things and I keep ending up in the middle of the dependency web trying to look up down and adjacent